Setting up the Development Environment

$ANDROID_HOME = /Library/Android/ SDK; $ANDROID_HOME = /Library/Android/ SDK; $ANDROID_HOME = /Library/Android/ SDK

Start configuring the Rust development environment

curl https://sh.rustup.sh -sSf | shCopy the code

The Rustup tool automatically adds Rust’s tools to the PATH.

Add the target compilation tool chain below

rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-androidCopy the code

Let’s start configuring the NDK development environment.

mkdir ~/.NDK

$(ANDROID_HOME)/ndk-bundle/build/tools/make_standalone_toolchain.py --api 26 --arch arm64 --install-dir ~/.NDK/arm64;
$(ANDROID_HOME)/ndk-bundle/build/tools/make_standalone_toolchain.py --api 26 --arch arm --install-dir ~/.NDK/arm;
$(ANDROID_HOME)/ndk-bundle/build/tools/make_standalone_toolchain.py --api 26 --arch x86 --install-dir ~/.NDK/x86;Copy the code

After configuration we write the information to ~/.cargo/config

[target.aarch64-linux-android]
ar = ".NDK/arm64/bin/aarch64-linux-android-ar"
linker = ".NDK/arm64/bin/aarch64-linux-android-clang"

[target.armv7-linux-androideabi]
ar = ".NDK/arm/bin/arm-linux-androideabi-ar"
linker = ".NDK/arm/bin/arm-linux-androideabi-clang"

[target.i686-linux-android]
ar = ".NDK/x86/bin/i686-linux-android-ar"
linker = ".NDK/x86/bin/i686-linux-android-clang"Copy the code

Hello World

Configure the above development environment can start to write code, this example we will write a Hello World program to verify.

Create the project first

mkdir rust-android-example
cd rust-android-example

cargo new rust --lib
cd rustCopy the code

Cargo. Toml code

[dependencies] jni = {version = "0 ", default-features = false} [profile.release] lto = true [lib] name="rust" crate-type=["cdylib"]Copy the code

Lib. Rs code

#! [cfg(target_os="android")] #! [allow(non_snake_case)] use std::ffi::{CString, CStr}; use jni::JNIEnv use jni::objects::{JObject, JString}; use jin::sys::{jstring}; #[no_mangle] pub unsafe extern fn Java_com_example_android_MainActivity_hello(env: JNIEnv, _: JObject, j_recipient: JString) -> jstring { let recipient = CString::from( CStr::from_prt(env.get_string(j_recipient).unwrap().as_ptr()) ); let output = env.new_string("Hello ".to_owned() + recipient.to_str().unwrap()).unwrap(); output.into_inner() }Copy the code

#[CFG (target_OS =”android”)] : enable only when target platform is Android, enable #[allow(non_snake_case)]

You are now ready to generate the Android lib.

// cargo-build.sh
cargo build --target aarch64-linux-android --release
cargo build --target armv7-linux-androideabi --release
cargo build --target i686-linux-android --releaseCopy the code

Android development

Create project with Android Studio, company domain as example.com needs to be the same as lib.rs in Rust above, project name is Android (copy script later)

cd rust-android-example

mkdir android/app/src/main/jniLibs
mkdir android/app/src/main/jniLibs/arm64-v8a
mkdir android/app/src/main/jniLibs/armeabi-v7a
mkdir android/app/src/main/jniLibs/x86

cp rust/target/aarch64-linux-android/release/librust.so android/app/src/main/jniLibs/arm64-v8a/librust.so
cp rust/target/armv7-linux-androideabi/release/librust.so android/app/src/main/jniLibs/armeabi-v7a/librust.so
cp rust/target/i686-linux-android/release/librust.so android/app/src/main/jniLibs/x86/librust.soCopy the code

Copy the target generated above to the Android directory

MainActivity.java

public class MainActivity extends AppCompatActivity { // Used to load the 'native-lib' library on application startup. static { System.loadLibrary("rust"); } public native String hello(String to); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Example of a call to a native method TextView tv = (TextView) findViewById(R.id.sample_text); tv.setText(hello("android")); }}Copy the code

Just launch and you’ll see Hello Android displayed on your phone.