| 1 | # Android native cross-compilation |
| 2 | |
| 3 | `cargo.sh` builds the ARM64 Rust half with an explicitly installed Android NDK |
| 4 | and Android API level. Run it from the checkout root. It does not install |
| 5 | tools, accept licenses, change the |
| 6 | SDK, or require Java. |
| 7 | |
| 8 | ```sh |
| 9 | rustup target add aarch64-linux-android |
| 10 | ANDROID_NDK_HOME=/path/to/android-ndk-r30 \ |
| 11 | CARGO_BUILD_JOBS=2 platform/android/cargo.sh 24 check -p mobile |
| 12 | ANDROID_NDK_HOME=/path/to/android-ndk-r30 \ |
| 13 | CARGO_BUILD_JOBS=2 platform/android/cargo.sh 24 build -p mobile |
| 14 | ``` |
| 15 | |
| 16 | API 24 here is a cross-compilation test target; the app's minimum supported |
| 17 | Android version is not decided by this lane. The chosen NDK must supply |
| 18 | `aarch64-linux-android<API>-clang`. macOS uses the NDK's universal |
| 19 | `darwin-x86_64` tools; x86_64 Linux uses `linux-x86_64`. |
| 20 | |
| 21 | The native static library is `target/android/aarch64-linux-android/debug/libmobile.a` |
| 22 | (or `release/libmobile.a` with `--release`). `CARGO_TARGET_DIR` can select another |
| 23 | shared build cache. Clean local package artifacts with `tools/cargo_fresh.py` |
| 24 | before verifying a different checkout against that cache. |
| 25 | |
| 26 | This lane checks and builds native code. `apps/android/build.py` builds the app on it: |
| 27 | it links the shared library with the same NDK, checks its alignment for 16 KB |
| 28 | page-size devices, and packages and signs the APK. |
| 29 | |
| 30 | Tool selection and prerequisite failures can be checked without an NDK: |
| 31 | |
| 32 | ```sh |
| 33 | python3 -m unittest discover -s tools -p 'test_android_toolchain.py' |
| 34 | ``` |
| 35 | |
| 36 | NDK compiler conventions: |
| 37 | https://developer.android.com/ndk/guides/other_build_systems |