|
| 1 | +# Mobile Example |
| 2 | + |
| 3 | +See the [main examples README](../) for general information about Bevy's examples. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +This directory contains an example project structure when developing for Android and iOS. |
| 8 | +These platforms require special SDKs, toolchains, build configuration, etc. |
| 9 | + |
| 10 | +Here we show you what you need to get started. |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## Android |
| 15 | + |
| 16 | +### Setup |
| 17 | + |
| 18 | +```sh |
| 19 | +rustup target add aarch64-linux-android |
| 20 | +cargo install cargo-ndk |
| 21 | +``` |
| 22 | + |
| 23 | +The Android SDK must be installed, and the environment variable `ANDROID_SDK_ROOT` set to the root Android `sdk` folder. |
| 24 | + |
| 25 | +When using `NDK (Side by side)`, the environment variable `ANDROID_NDK_ROOT` must also be set to one of the NDKs in `sdk\ndk\[NDK number]`. |
| 26 | + |
| 27 | +Alternatively, you can install Android Studio. |
| 28 | + |
| 29 | +### Build & Run |
| 30 | + |
| 31 | +To build an Android app, you first need to build shared object files for the target architecture with `cargo-ndk`: |
| 32 | + |
| 33 | +```sh |
| 34 | +cargo ndk -t <target_name> -o <project_name>/app/src/main/jniLibs build |
| 35 | +``` |
| 36 | + |
| 37 | +For example, to compile to a 64-bit ARM platform: |
| 38 | + |
| 39 | +```sh |
| 40 | +cargo ndk -t arm64-v8a -o android_example/app/src/main/jniLibs build |
| 41 | +``` |
| 42 | + |
| 43 | +Setting the output path ensures the shared object files can be found in target-specific directories under `jniLibs` where the JNI can find them. |
| 44 | + |
| 45 | +See the `cargo-ndk` [README](https://crates.io/crates/cargo-ndk) for other options. |
| 46 | + |
| 47 | +After this you can build it with `gradlew`: |
| 48 | + |
| 49 | +```sh |
| 50 | +./gradlew build |
| 51 | +``` |
| 52 | + |
| 53 | +Or build it with Android Studio. |
| 54 | + |
| 55 | +Then you can test it in your Android project. |
| 56 | + |
| 57 | +#### About `libc++_shared.so` |
| 58 | + |
| 59 | +Bevy may require `libc++_shared.so` to run on Android, as it is needed by the `oboe` crate, but typically `cargo-ndk` does not copy this file automatically. |
| 60 | + |
| 61 | +To include it, you can manually obtain it from NDK source or use a `build.rs` script for automation, as described in the `cargo-ndk` [README](https://github.com/bbqsrc/cargo-ndk?tab=readme-ov-file#linking-against-and-copying-libc_sharedso-into-the-relevant-places-in-the-output-directory). |
| 62 | + |
| 63 | +Alternatively, you can modify project files to include it when building an APK. To understand the specific steps taken in this project, please refer to the comments within the project files for detailed instructions(`app/CMakeList.txt`, `app/build.gradle`, `app/src/main/cpp/dummy.cpp`). |
| 64 | + |
| 65 | +### Debugging |
| 66 | + |
| 67 | +You can view the logs with the following command: |
| 68 | + |
| 69 | +```sh |
| 70 | +adb logcat | grep 'RustStdoutStderr\|bevy\|wgpu' |
| 71 | +``` |
| 72 | + |
| 73 | +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 information. |
| 74 | + |
| 75 | +Sometimes, running the app complains about an unknown activity. This may be fixed by uninstalling the application: |
| 76 | + |
| 77 | +```sh |
| 78 | +adb uninstall org.bevyengine.example |
| 79 | +``` |
| 80 | + |
| 81 | +### Old phones |
| 82 | + |
| 83 | +In its examples, Bevy targets the minimum Android API that Play Store <!-- markdown-link-check-disable --> |
| 84 | +[requires](https://developer.android.com/distribute/best-practices/develop/target-sdk) to upload and update apps. <!-- markdown-link-check-enable --> |
| 85 | +Users of older phones may want to use an older API when testing. By default, Bevy uses [`GameActivity`](https://developer.android.com/games/agdk/game-activity), which only works for Android API level 31 and higher, so if you want to use older API, you need to switch to `NativeActivity`. |
| 86 | + |
| 87 | +To use `NativeActivity`, you need to edit it in `cargo.toml` manually like this: |
| 88 | + |
| 89 | +```toml |
| 90 | +bevy = { version = "0.14", default-features = false, features = ["android-native-activity", ...] } |
| 91 | +``` |
| 92 | + |
| 93 | +Then build it as the [Build & Run](#build--run) section stated above. |
| 94 | + |
| 95 | +#### About `cargo-apk` |
| 96 | + |
| 97 | +You can also build an APK with `cargo-apk`, a simpler and deprecated tool which doesn't support `GameActivity`. If you want to use this, there is a [folder](./mobile/android_basic) inside the mobile example with instructions. |
| 98 | + |
| 99 | +## iOS |
| 100 | + |
| 101 | +### Setup |
| 102 | + |
| 103 | +You need to install the correct rust targets: |
| 104 | + |
| 105 | +- `aarch64-apple-ios`: iOS devices |
| 106 | +- `x86_64-apple-ios`: iOS simulator on x86 processors |
| 107 | +- `aarch64-apple-ios-sim`: iOS simulator on Apple processors |
| 108 | + |
| 109 | +```sh |
| 110 | +rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-sim |
| 111 | +``` |
| 112 | + |
| 113 | +### Build & Run |
| 114 | + |
| 115 | +Using bash: |
| 116 | + |
| 117 | +```sh |
| 118 | +cd examples/mobile |
| 119 | +make run |
| 120 | +``` |
| 121 | + |
| 122 | +In an ideal world, this will boot up, install and run the app for the first |
| 123 | +iOS simulator in your `xcrun simctl list devices`. If this fails, you can |
| 124 | +specify the simulator device UUID via: |
| 125 | + |
| 126 | +```sh |
| 127 | +DEVICE_ID=${YOUR_DEVICE_ID} make run |
| 128 | +``` |
| 129 | + |
| 130 | +If you'd like to see xcode do stuff, you can run |
| 131 | + |
| 132 | +```sh |
| 133 | +open bevy_mobile_example.xcodeproj/ |
| 134 | +``` |
| 135 | + |
| 136 | +which will open xcode. You then must push the zoom zoom play button and wait |
| 137 | +for the magic. |
0 commit comments