Flutter error when building libflutter_rust_bridge_example for Android: Failed to load dynamic library 'libflutter_rust_bridge_example.so'

Viewed 195

I am trying build the flutter_rust_bridge project (frb_example/with_flutter) for Android, but i receive the following error multiple times when trying to run:

E/flutter (10161): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library 'libflutter_rust_bridge_example.so': dlopen failed: library "libflutter_rust_bridge_example.so" not found
E/flutter (10161): #0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:12:43)
E/flutter (10161): #1      new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:23:12)
E/flutter ( 9864): #2      dylib
package:flutter_rust_bridge_example/main.dart:19
E/flutter ( 9864): #3      dylib (package:flutter_rust_bridge_example/main.dart)
package:flutter_rust_bridge_example/main.dart:1
E/flutter ( 9864): #4      api
package:flutter_rust_bridge_example/main.dart:20
E/flutter ( 9864): #5      api (package:flutter_rust_bridge_example/main.dart)
package:flutter_rust_bridge_example/main.dart:1
E/flutter ( 9864): #6      _MyAppState._callExampleFfiOne
package:flutter_rust_bridge_example/main.dart:49
E/flutter ( 9864): #7      runPeriodically.<anonymous closure>
package:flutter_rust_bridge_example/off_topic_code.dart:126
E/flutter ( 9864): #8      _rootRunUnary (dart:async/zone.dart:1434:47)
E/flutter ( 9864): #9      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 9864): #10     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
E/flutter ( 9864): #11     _CustomZone.bindUnaryCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1281:26)
E/flutter ( 9864): #12     _rootRunUnary (dart:async/zone.dart:1442:13)
E/flutter ( 9864): #13     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 9864): #14     _CustomZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1265:26)
E/flutter ( 9864): #15     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
E/flutter ( 9864): #16     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)
E/flutter ( 9864): #17     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

Where is the mentioned libflutter_rust_bridge_example.so file supposed to be?

I also get the following warnings:

Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01
Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01

The android emulator (Nexus 5 android-x86) looks the following:

as you can see the image which should be generated by rust is missing. If I build for Windows everything works just fine:

enter image description here

Os: Windows 10

Flutter version: Flutter 3.0.0

rust version: 1.60.0

IDE to launch the emulator: Vs Code

IDE to create the emulator: Android Studio

2 Answers

I've ran into the same issue on my Arch Linux KDE Laptop. For me it was caused by forgetting to run the required previous step (see tutorial description):

cd ~/Development/git/flutter_rust_bridge/frb_example/with_flutter/rust
cargo ndk -o ../android/app/src/main/jniLibs build

and afterwards the step failing because of missing dependencies, which in my case was fixed by installing:

cargo install cargo-ndk
rustup target add armv7-linux-androideabi
rustup target add aarch64-linux-android

(the required target architecture depends on the phone model / emulator used)

if that still doesn't fix it, maybe try to reset the repository into a freshly cloned state:

cd ~/Development/git/flutter_rust_bridge
git clean -d -x -f

I had the exact same problem. I am using a Kindle Fire 7.

It turns out that the instructions assume that the Android device has a 64-bit x86 CPU. The Kindle Fire 7 uses an ARM Cortex A7 instead. This requires us to use the armv7-linux-androideabi target.

So in the rust directory I typed:

rustup target add armv7-linux-androideabi
cargo ndk -t armv7-linux-androideabi -o ..\android\app\src\main\jniLibs build

From there, everything worked perfectly. The key is to figure out what CPU your specific device is running, then select the correct target for cargo to build the executable.

Note that there's no disadvantage to including as many targets as possible. Here is a list of targets I found in the user guide for flutter_rust_bridge:

  • aarch64-linux-android
  • armv7-linux-androideabi
  • x86_64-linux-android
  • i686-linux-android
Related