Skip to content

Commit

Permalink
WIP support compiling example/hello_world for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
paulotten committed Aug 9, 2022
1 parent 7c25a92 commit 7ba710e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 39 deletions.
7 changes: 7 additions & 0 deletions examples/hello_world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ publish = false

[dependencies]
eframe = { path = "../../eframe" }

# This dependency will only be included when targeting Android
[target.'cfg(target_os = "android")'.dependencies]
ndk-glue = "0.6.2"

[lib]
crate-type = ["lib", "cdylib"]
26 changes: 26 additions & 0 deletions examples/hello_world/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
## Linux, MacOS, Windows

```sh
cargo run -p hello_world
```

## Android

WIP, based on https://github.com/rust-windowing/android-ndk-rs#quick-start-hello-world-crate-on-android

Install the Android NDK and SDK as per https://github.com/rust-windowing/android-ndk-rs#1-install-the-android-ndk-and-sdk

Install Android target

```sh
rustup target add aarch64-linux-android
```

Install cargo-apk

```sh
cargo install cargo-apk
```

Run the Android app

```sh
cargo apk run --lib
```
47 changes: 47 additions & 0 deletions examples/hello_world/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use eframe::egui;

// Android entry point
#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn main() {
run()
}

pub fn run() {
let options = eframe::NativeOptions::default();
eframe::run_native(
"My egui App",
options,
Box::new(|_cc| Box::new(MyApp::default())),
);
}

struct MyApp {
name: String,
age: u32,
}

impl Default for MyApp {
fn default() -> Self {
Self {
name: "Arthur".to_owned(),
age: 42,
}
}
}

impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("My egui Application");
ui.horizontal(|ui| {
ui.label("Your name: ");
ui.text_edit_singleline(&mut self.name);
});
ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
if ui.button("Click each year").clicked() {
self.age += 1;
}
ui.label(format!("Hello '{}', age {}", self.name, self.age));
});
}
}
41 changes: 2 additions & 39 deletions examples/hello_world/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,6 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release

use eframe::egui;

// Linux, MacOS, Windows entry point
fn main() {
let options = eframe::NativeOptions::default();
eframe::run_native(
"My egui App",
options,
Box::new(|_cc| Box::new(MyApp::default())),
);
}

struct MyApp {
name: String,
age: u32,
}

impl Default for MyApp {
fn default() -> Self {
Self {
name: "Arthur".to_owned(),
age: 42,
}
}
}

impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("My egui Application");
ui.horizontal(|ui| {
ui.label("Your name: ");
ui.text_edit_singleline(&mut self.name);
});
ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
if ui.button("Click each year").clicked() {
self.age += 1;
}
ui.label(format!("Hello '{}', age {}", self.name, self.age));
});
}
hello_world::run()
}

0 comments on commit 7ba710e

Please sign in to comment.