Rust for Mobile Development
Since the creation of this blog, I’ve been focusing on mobile and web development, for nearly a decade. At the same time, the popularity of Rust is growing quickly.
There’re not so much community resources about mobile development with Rust, though Mozilla has been using Rust for Firefox Android/iOS for a long time. After investigating their Rust projects, it is a good time to write a blog about it.
If you’d like to see paid Rust tutorial for mobile development at 2023, please contact my agent:
catstudio.app[at]gmail.com
Is Rust ready for mobile development?
The short answer is “yes”.
Most well-known showcases of Rust are about backend development. However, you can also find many cases about its usage in the client-side development.
If you’d like to see more, here are great references:
- https://www.rust-lang.org/production/users
- https://www.reddit.com/r/rust/comments/ea53vh/production_usage_of_rust_in_androidios/
Key pieces of the puzzle
You can write a simple Rust library with C bindings to be used in Swift/Objective-C or Java/Kotlin (JNI). However, it is not an easy way. C bindings mean a lot of boilerplate code, and it’s hard to maintain.
Fortunately, Mozilla built two key frameworks which help a lot:
- UniFFI (https://github.com/mozilla/uniffi-rs): Automatic codegen for Rust APIs
- Rust Android Gradle Plugin (https://github.com/mozilla/rust-android-gradle): Handle complicated NDK build script seamlessly
Example of mobile development with Rust
I built a simple codebase for a mobile app using Rust: https://github.com/imWildCat/uniffi-rs-fullstack-examples. I believe the code explains itself. But it should be good to mention some tricks.
iOS
For iOS, you need to build the binary for each target, including simulator and device (Intel and Apple CPUs, respectively).
Usually, you need to build for this matrix:
Target | CPU |
---|---|
Simulator | x86_64 |
Simulator | aarch64 |
Device | x86_64 |
Device | aarch64 |
For the aarch64 simulator toolchain of Rust, it is not supported by the stable channel. So, you need to install the nightly toolchain with source code. Similar things apply to most Mac Catalyst targets:
rustup toolchain install nightly
rustup target add aarch64-apple-ios-sim --toolchain nightly
rustup target add aarch64-apple-ios x86_64-apple-ios
rustup component add rust-src --toolchain nightly
More information about the platform support of Rust can be found at https://doc.rust-lang.org/nightly/rustc/platform-support.html.
Android
The only thing that we need to be carefully about is Python 2 removal on macOS 12.3.
If you’d like to build Android libraries using NDK on macOS 12.3 and later, you’d better to build Python 2.x from source and link it to /usr/local/bin/python
. Otherwise, the build is likely to fail.
It’s sad to see that Python 2 is still a dependency of the NDK toolchain in 2022.
Summary
Although it is still an early stage of Rust development for mobile, this language has a great potential. To me, the most attractive feature is the zero-cost abstraction of Rust. This means the binary generated can be small enough and its performance is great. Since many great teams like Mozilla have been using it for mobile applications for several years, it is a good time to start using it.