Example of a simple bridge between Android Java JNI and a Rust library.
This library focuses on simple use cases (eg get a JArray from a JString,...). JStringArray are not yet part of the wrapper. I am still working on that, but as the length of a JArray doesn't have to be known at compile time with our wrapper that will work in most circumstances.
Rust functions are tested. Testing the wrapper functions in Rust is a bit more of a hassle. I might get into that another time.
- Install git (see here how to install git) and Rust.
- Install Android Studio and add the ANDROID_HOME and PATH varaible
- Set up a NDK toolchain
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android
gedit ~/.cargo/config
Adjust and add the following part (Identify your NDK folder with the prebuilt files in your Android SDK location (similar to the paths shown))
[target.aarch64-linux-android]
ar = "/home/username/Android/Sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-ar"
linker = "/home/username/Android/Sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android29-clang"
[target.armv7-linux-androideabi]
ar = "/home/username/Android/Sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ar"
linker = "/home/username/Android/Sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi29-clang"
[target.i686-linux-android]
ar = "/home/username/Android/Sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android-ar"
linker = "/home/username/Android/Sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android28-clang"
save and exit file.
git clone https://github.com/mad-de/rust_JNI_Android_example_wrapper
cd rust_JNI_Android_example_wrapper
./buildScript.command
- Load android folder as a new project in Android SDK and run / build apk
cd rust
cargo test