Tuesday, September 6, 2022

Running Rust and Flutter on a Kindle Fire 7

 Due to my strong interest in both Rust and mobile apps, I've been learning to use the flutter_rust_bridge package to build mobile apps using Rust and Flutter. They have a pretty clever example in their user guide in which the Rust code generates a Mandelbrot set which is rendered in the Flutter app. I wanted to try it out on an Android device. For development purposes, I'm using a Kindle Fire 7.

Unfortunately, their instructions are a bit cryptic, and in some places not entirely correct for the Kindle Fire 7 or for building on Windows. Here is what I had to do to get it working:

  • I cloned their GitHub repo as instructed. If you use an IDE, I suggest opening it up in Android Studio.
  • In the CI Workflow, I examined the flutter_android_test section. The bullets below described what I used or had to adapt.
    • I had installed NDK some time ago. The instructions there are for a Unix platform. For Windows, Android provides a useful guide. I think this is what I did, but I honestly don't remember for sure.
    • I already had Rust and Flutter installed. 
    • I next installed the cargo-ndk program: cargo install cargo-ndk
    • Here is where it was necessary to diverge from their instructions. The Kindle Fire 7 uses an ARM Cortex A7 CPU. So to target that CPU, I typed: rustup target add armv7-linux-androideabi in the rust subdirectory. This downloads the necessary files for rustc to cross-compile to that specific Android architecture.
    • Note that, other than disk space, there is no particular drawback to including as many distinct Android targets as you'd like. Each one you compile will be included in the APK, and at runtime it will try to select the correct .so file for the architecture on which it is running. 
    • At this point, I was ready to compile: cargo ndk -t armv7-linux-androideabi -o ..\android\app\src\main\jniLibs build
  • Having completed the above setup, flutter run built the APK, uploaded it to my device, and executed it perfectly.
I realized what the problem was after finding this answer on StackOverflow. I added my own answer as well, then wrote this blog post to add a few more details for any other Android Rust enthusiasts out there!