Building cross platform library with Rust for ios and android
I’ll build a cross platform library for IOS and Android using Rust.
I’ll create a platform agnostic rust library which will be used by the IOS specific Rust library and Android Rust Library. The native platforms will link respective Rust libraries in the native project.
We’ll just return hello from the rust world in this example. The main objective is how to setup the structure of the library and config. You can take this forward once the setup is done.
Setup
- Make sure you have Android Studio installed
- Make sure you have XCode installed
- Install rust
- Make sure the Cargo is installed
- For IOS: Make sure cargo-lipo and cbindgen is installed. It will be used to create IOS build and generate C headers respectively.
cargo install cargo-lipo
cargo install cbindgen
- For IOS: Add target devices
rustup target add aarch64-apple-ios x86_64-apple-ios
- For Android: Add target devices
rustup target add armv7-linux-androideabi
rustup target add i686-linux-android
rustup target add aarch64-linux-android
rustup target add x86_64-linux-android
rustup target add x86_64-unknown-linux-gnu
rustup target add x86_64-apple-darwin
rustup target add x86_64-pc-windows-gnu
rustup target add x86_64-pc-windows-msvc
Github Repo
In case you want to try running the project before reading ahead, try the code from below repo.
Let’s Create Common Rust Library
Create hello project using cargo
$ cargo new hello --lib
We’ll add a new module hello to the hello library and use greetings_from_rust()
method to get our text.
IOS Rust Library
Let’s create the IOS rust library with command
cargo new helloios --lib
Android Rust Library
cargo new helloandroid --lib
A word on JNI
Allows Java code that runs inside a Java Virtual Machine (VM) to inter-operate with applications and libraries written in other programming languages, such as Rust, C, C++, and assembly. For details refer
https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html
https://developer.android.com/training/articles/perf-jni
Create Android Application
I’ll use the package rust-android-gradle which would also build and link the package.
Create IOS App
Create a new IOS application. Now building and linking with IOS app is not as simple as Android. We have to build and link the binaries ourselves using steps mentioned below.
Building the Rust IOS library
I have added a script to build and generate the headers, these files will then be copied to the IOS App project.
Linking the IOS Rust library
Add the generated library under ios/libs/librgpwrapperios.a
to the general settings.
Add search paths for the headers and the library
Add Objective C bridging header as below
Invoke the Rust Api
Now we invoke the rust api when a tap gesture is triggered on the text view.
Now you run and extend the library and it’s modules anyway you want.
Logging
Android
You can view any compile time errors in the android Build Tab.
Unfortunately rust macros print!
or println!
does not print out any logs to the Logcat
.
I’ll recommend using Rust plugin android_logger to achieve this.
IOS
No special library is needed for this, the print!
and println!
macros work fine.
Rust web framework comparison
You can find comparison of some web frameworks written in Rust here.
In case you find some better plugins or approaches towards configuration. Please make sure to drop a comment to this article.
Thanks!