Skip to content

Commit d4e4a92

Browse files
committed
android - fix issues other than the rendering (bevyengine#5130)
# Objective - Make Bevy work on android ## Solution - Update android metadata and add a few more - Set the target sdk to 31 as it will soon (in august) be the minimum sdk level for play store - Remove the custom code to create an activity and use ndk-glue macro instead - Delay window creation event on android - Set the example with compatibility settings for wgpu. Those are needed for Bevy to work on my 2019 android tablet - Add a few details on how to debug in case of failures - Fix running the example on emulator. This was failing because of the name of the example Bevy still doesn't work on android with this, audio features need to be disabled because of an ndk-glue version mismatch: rodio depends on 0.6.2, winit on 0.5.2. You can test with: ``` cargo apk run --release --example android_example --no-default-features --features "bevy_winit,render" ```
1 parent 96f0ebb commit d4e4a92

File tree

7 files changed

+71
-36
lines changed

7 files changed

+71
-36
lines changed

.github/workflows/validation-jobs.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,14 @@ jobs:
5252
target/
5353
key: ${{ runner.os }}-cargo-build-android-${{ hashFiles('**/Cargo.toml') }}
5454

55-
- name: Uninstall android-31
56-
run: $ANDROID_SDK_ROOT/cmdline-tools/latest/bin/sdkmanager --uninstall "platforms;android-31"
57-
5855
- name: Install Android targets
5956
run: rustup target add aarch64-linux-android armv7-linux-androideabi
6057

6158
- name: Install Cargo APK
6259
run: cargo install --force cargo-apk
6360

6461
- name: Build APK
65-
run: cargo apk build --example android
62+
run: cargo apk build --example android_example
6663

6764
run-examples-on-windows:
6865
runs-on: windows-latest

Cargo.toml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,19 +1467,22 @@ hidden = true
14671467
# Android
14681468
[[example]]
14691469
crate-type = ["cdylib"]
1470-
name = "android"
1470+
name = "android_example"
14711471
path = "examples/android/android.rs"
14721472

1473-
[package.metadata.example.android]
1473+
[package.metadata.example.android_example]
14741474
hidden = true
14751475

14761476
[package.metadata.android]
1477-
apk_label = "Bevy Example"
1477+
package = "org.bevyengine.example"
1478+
apk_name = "bevyexample"
14781479
assets = "assets"
14791480
resources = "assets/android-res"
14801481
build_targets = ["aarch64-linux-android", "armv7-linux-androideabi"]
1481-
min_sdk_version = 16
1482-
target_sdk_version = 29
1482+
1483+
[package.metadata.android.sdk]
1484+
target_sdk_version = 31
14831485

14841486
[package.metadata.android.application]
14851487
icon = "@mipmap/ic_launcher"
1488+
label = "Bevy Example"

crates/bevy_derive/src/bevy_main.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,11 @@ pub fn bevy_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
1010
);
1111

1212
TokenStream::from(quote! {
13-
#[no_mangle]
13+
// use ndk-glue macro to create an activity: https://github.com/rust-windowing/android-ndk-rs/tree/master/ndk-macro
1414
#[cfg(target_os = "android")]
15-
unsafe extern "C" fn ANativeActivity_onCreate(
16-
activity: *mut std::os::raw::c_void,
17-
saved_state: *mut std::os::raw::c_void,
18-
saved_state_size: usize,
19-
) {
20-
bevy::ndk_glue::init(
21-
activity as _,
22-
saved_state as _,
23-
saved_state_size as _,
24-
main,
25-
);
15+
#[cfg_attr(target_os = "android", bevy::ndk_glue::main(backtrace = "on", ndk_glue = "bevy::ndk_glue"))]
16+
fn android_main() {
17+
main()
2618
}
2719

2820
#[no_mangle]

crates/bevy_winit/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ impl Plugin for WinitPlugin {
4848
#[cfg(target_arch = "wasm32")]
4949
app.add_plugin(web_resize::CanvasParentResizePlugin);
5050
let event_loop = EventLoop::new();
51+
#[cfg(not(target_os = "android"))]
5152
let mut create_window_reader = WinitCreateWindowReader::default();
53+
#[cfg(target_os = "android")]
54+
let create_window_reader = WinitCreateWindowReader::default();
5255
// Note that we create a window here "early" because WASM/WebGL requires the window to exist prior to initializing
5356
// the renderer.
57+
#[cfg(not(target_os = "android"))]
5458
handle_create_window_events(&mut app.world, &event_loop, &mut create_window_reader.0);
5559
app.insert_resource(create_window_reader)
5660
.insert_non_send_resource(event_loop);

examples/README.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,32 +346,47 @@ When using `NDK (Side by side)`, the environment variable `ANDROID_NDK_ROOT` mus
346346
To run on a device setup for Android development, run:
347347

348348
```sh
349-
cargo apk run --example android
349+
cargo apk run --example android_example
350350
```
351351

352-
:warning: At this time Bevy does not work in Android Emulator.
353-
354352
When using Bevy as a library, the following fields must be added to `Cargo.toml`:
355353

356354
```toml
357355
[package.metadata.android]
358356
build_targets = ["aarch64-linux-android", "armv7-linux-androideabi"]
359-
target_sdk_version = 29
360-
min_sdk_version = 16
357+
358+
[package.metadata.android.sdk]
359+
target_sdk_version = 31
361360
```
362361

363362
Please reference `cargo-apk` [README](https://crates.io/crates/cargo-apk) for other Android Manifest fields.
364363

364+
### Debugging
365+
366+
You can view the logs with the following command:
367+
368+
```sh
369+
adb logcat | grep 'RustStdoutStderr\|bevy\|wgpu'
370+
```
371+
372+
In case of an error getting a GPU or setting it up, you can try settings logs of `wgpu_hal` to `DEBUG` to get more informations.
373+
374+
Sometimes, running the app complains about an unknown activity. This may be fixed by uninstalling the application:
375+
376+
```sh
377+
adb uninstall org.bevyengine.example
378+
```
379+
365380
### Old phones
366381

367-
Bevy by default targets Android API level 29 in its examples which is the <!-- markdown-link-check-disable -->
382+
Bevy by default targets Android API level 31 in its examples which is the <!-- markdown-link-check-disable -->
368383
[Play Store's minimum API to upload or update apps](https://developer.android.com/distribute/best-practices/develop/target-sdk). <!-- markdown-link-check-enable -->
369384
Users of older phones may want to use an older API when testing.
370385

371386
To use a different API, the following fields must be updated in Cargo.toml:
372387

373388
```toml
374-
[package.metadata.android]
389+
[package.metadata.android.sdk]
375390
target_sdk_version = >>API<<
376391
min_sdk_version = >>API or less<<
377392
```

examples/README.md.tpl

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,32 +98,47 @@ When using `NDK (Side by side)`, the environment variable `ANDROID_NDK_ROOT` mus
9898
To run on a device setup for Android development, run:
9999

100100
```sh
101-
cargo apk run --example android
101+
cargo apk run --example android_example
102102
```
103103

104-
:warning: At this time Bevy does not work in Android Emulator.
105-
106104
When using Bevy as a library, the following fields must be added to `Cargo.toml`:
107105

108106
```toml
109107
[package.metadata.android]
110108
build_targets = ["aarch64-linux-android", "armv7-linux-androideabi"]
111-
target_sdk_version = 29
112-
min_sdk_version = 16
109+
110+
[package.metadata.android.sdk]
111+
target_sdk_version = 31
113112
```
114113

115114
Please reference `cargo-apk` [README](https://crates.io/crates/cargo-apk) for other Android Manifest fields.
116115

116+
### Debugging
117+
118+
You can view the logs with the following command:
119+
120+
```sh
121+
adb logcat | grep 'RustStdoutStderr\|bevy\|wgpu'
122+
```
123+
124+
In case of an error getting a GPU or setting it up, you can try settings logs of `wgpu_hal` to `DEBUG` to get more informations.
125+
126+
Sometimes, running the app complains about an unknown activity. This may be fixed by uninstalling the application:
127+
128+
```sh
129+
adb uninstall org.bevyengine.example
130+
```
131+
117132
### Old phones
118133

119-
Bevy by default targets Android API level 29 in its examples which is the <!-- markdown-link-check-disable -->
134+
Bevy by default targets Android API level 31 in its examples which is the <!-- markdown-link-check-disable -->
120135
[Play Store's minimum API to upload or update apps](https://developer.android.com/distribute/best-practices/develop/target-sdk). <!-- markdown-link-check-enable -->
121136
Users of older phones may want to use an older API when testing.
122137

123138
To use a different API, the following fields must be updated in Cargo.toml:
124139

125140
```toml
126-
[package.metadata.android]
141+
[package.metadata.android.sdk]
127142
target_sdk_version = >>API<<
128143
min_sdk_version = >>API or less<<
129144
```

examples/android/android.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
use bevy::prelude::*;
1+
use bevy::{
2+
prelude::*,
3+
render::settings::{WgpuSettings, WgpuSettingsPriority},
4+
};
25

36
// the `bevy_main` proc_macro generates the required android boilerplate
47
#[bevy_main]
58
fn main() {
69
App::new()
10+
// This configures the app to use the most compatible rendering settings.
11+
// They help with compatibilty with as many devices as possible.
12+
.insert_resource(WgpuSettings {
13+
priority: WgpuSettingsPriority::Compatibility,
14+
..default()
15+
})
716
.add_plugins(DefaultPlugins)
817
.add_startup_system(setup)
918
.run();

0 commit comments

Comments
 (0)