Before you can compile for Android, you need to setup your environment. This needs to be done only once per system.
-
Install
rustup
. -
Run
rustup target add arm-linux-androideabi
, or any other target that you want to compile to. -
Install the Java JDK and Ant (on Ubuntu,
sudo apt-get install openjdk-8-jdk ant
) -
Download and unzip the Android NDK
-
Download and unzip the Android SDK (under SDK Tools Only at the bottom)
-
Update the SDK:
./android-sdk-linux/tools/android update sdk -u
-
Install
cargo-apk
withcargo install cargo-apk
.
Run cargo apk
.
This will build an Android package in target/android-artifacts/build/bin
.
Start the emulator, then run:
adb install -r target/your_crate
This will install your application on the emulator.
An application is not very useful if it doesn't have access to the screen, the user inputs, etc.
The android_glue
crate provides FFI with the Android environment for things that are not in
the stdlib.
The build process works by invoking cargo rustc
and:
- Always compiles your crate as a shared library.
- Injects the
android_native_app_glue
file provided by the Android NDK. - Injects some glue libraries in Rust, which ties the link between
android_native_app_glue
and themain
function of your crate.
This first step outputs a shared library, and is run once per target architecture.
The command then sets up an Android build environment, which includes some Java code, in
target/android-artifacts
and puts the shared libraries in it. Then it runs ant
.