- Install Android SDK & NDK. Set Up ANDROID_HOME & NDK_HOME environment variables.
- We need to use nightly Rust & add targest related to Android. Run the below comamnds :
rustup target add armv7-linux-androideabi # for arm
rustup target add i686-linux-android # for x86
rustup target add aarch64-linux-android # for arm64
rustup target add x86_64-linux-android # for x86_64
rustup override set nightly
Now, we can follow miltiple ways to cross compile Rust code for Android OS.
a. Use cargo-ndk : https://github.com/bbqsrc/cargo-ndk ( simple & easy)
b. Use Mozilla gradle plugin(suitable for Android project) : https://github.com/mozilla/rust-android-gradle
c. Add the target linker paths for targets in ~/.cargo/config.toml. (Easiest way for anyone; now we need to configure anything in actual project config). Now, we can compile programs directl with cargo , Example :
cargo build --target aarch64-linux-android --target armv7-linux-androideabi --target x86_64-linux-android --target i686-linux-android --target x86_64-unknown-linux-gnu
I found this step easier and simpler as we need not install anything for this and only one time config step is needed.
d. Use build.sh script in each project as provided above.
References : https://gendignoux.com/blog/2022/10/24/rust-library-android.html
https://github.com/bbqsrc/cargo-ndk
https://maciejglowka.com/blog/building-games-for-android-with-rust/
https://github.com/mozilla/rust-android-gradle
https://medium.com/@1ok1/android-rust-ndk-starter-dfb6f739ceed
https://www.reddit.com/r/rust/comments/16ngvhx/how_to_contol_the_link_stage_of_rust_build_system/
https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#target-applies-to-host
There were many others pages which I read.