fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
3.64k stars 256 forks source link

Cant use Sqlite with Android (only emulator issue probably) #1844

Closed NightBlaze closed 2 weeks ago

NightBlaze commented 2 months ago

Describe the bug

Minimal reproducible project is here https://github.com/NightBlaze/frb_sqlite

I want to use Sqlite as storage for my app and all works fine on iOS Simulator and iOS real device but doesn't work on Android emulator x86_64. I googled the issue and it seems that it's related only to x86_64 arch and all works on real Android devices (arm) but I don't have real Android device and can't test it.

I tried to use diesel and sqlx crates. Results are same.

When I run app on Android emulator it logs an error to console (see Logs) and doesn't start.

It's not FRB related issue but it will be nice if we find a solution for other who will use Sqlite + Android.

Also need to be sure that it's only emulator issue.

Steps to reproduce

Clone https://github.com/NightBlaze/frb_sqlite and run on Android emulator.

Logs

-> % flutter run                                  
Launching lib/main.dart on sdk gphone64 x86 64 in debug mode...
Resolving dependencies...
+ adaptive_number 1.0.0
+ args 2.4.2
+ async 2.11.0
+ build_tool 1.0.0 from path /Users/user/Work/frb_sqlite/rust_builder/cargokit/gradle/../build_tool
+ collection 1.18.0
+ convert 3.1.1
+ crypto 3.0.3
+ ed25519_edwards 0.3.1
+ fixnum 1.1.0
+ github 9.17.0 (9.24.0 available)
+ hex 0.2.0
+ http 1.1.0 (1.2.1 available)
+ http_parser 4.0.2
+ json_annotation 4.8.1
+ logging 1.2.0
+ meta 1.12.0
+ path 1.8.0 (1.9.0 available)
+ petitparser 5.4.0 (6.0.2 available)
+ source_span 1.10.0
+ string_scanner 1.2.0
+ term_glyph 1.2.1
+ toml 0.14.0 (0.15.0 available)
+ typed_data 1.3.2
+ version 3.0.0 (3.0.2 available)
+ yaml 3.1.2
Changed 25 dependencies!
6 packages have newer versions incompatible with dependency constraints.
Try `dart pub outdated` for more information.
Compiling bin/build_tool_runner.dart to kernel file bin/build_tool_runner.dill.
INFO: Precompiled binaries are disabled
INFO: Building rust_lib_frb_sqlite for x86_64-linux-android
INFO: Building rust_lib_frb_sqlite for i686-linux-android
INFO: Building rust_lib_frb_sqlite for x86_64-linux-android
Running Gradle task 'assembleDebug'...                            245,9s
✓  Built build/app/outputs/flutter-apk/app-debug.apk.
Installing build/app/outputs/flutter-apk/app-debug.apk...        2 971ms
E/flutter ( 5222): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library 'librust_lib_frb_sqlite.so': dlopen failed: cannot locate symbol "__extenddftf2" referenced by "/data/app/~~Fd1yr5C6LdrkLyzJ--Bjbg==/com.example.frb_sqlite-TarkLSXer1ZxQ-Ec6U9CVA==/lib/x86_64/librust_lib_frb_sqlite.so"...
E/flutter ( 5222): #0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:11:43)
E/flutter ( 5222): #1      new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:22:12)
E/flutter ( 5222): #2      new ExternalLibrary.open (package:flutter_rust_bridge/src/platform_types/_io.dart:39:47)
E/flutter ( 5222): #3      loadExternalLibraryRaw (package:flutter_rust_bridge/src/loader/_io.dart:56:28)
E/flutter ( 5222): #4      loadExternalLibrary (package:flutter_rust_bridge/src/loader/_io.dart:14:10)
E/flutter ( 5222): #5      BaseEntrypoint._loadDefaultExternalLibrary (package:flutter_rust_bridge/src/main_components/entrypoint.dart:107:13)
E/flutter ( 5222): #6      BaseEntrypoint.initImpl (package:flutter_rust_bridge/src/main_components/entrypoint.dart:53:31)
E/flutter ( 5222): #7      RustLib.init (package:frb_sqlite/src/rust/frb_generated.dart:25:20)
E/flutter ( 5222): #8      main (package:frb_sqlite/main.dart:9:17)
E/flutter ( 5222): #9      _runMain.<anonymous closure> (dart:ui/hooks.dart:301:23)
E/flutter ( 5222): #10     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
E/flutter ( 5222): #11     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
E/flutter ( 5222): 
Syncing files to device sdk gphone64 x86 64...                     138ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

A Dart VM Service on sdk gphone64 x86 64 is available at: http://127.0.0.1:62774/fzcwN2HtsUc=/

Expected behavior

No response

Generated binding code

No response

OS

macOS

Version of flutter_rust_bridge_codegen

flutter_rust_bridge_codegen 2.0.0-dev.28

Flutter info

-> % flutter doctor -v
[✓] Flutter (Channel stable, 3.19.4, on macOS 14.4.1 23E224 darwin-x64, locale ru-RU)
    • Flutter version 3.19.4 on channel stable at /Users/user/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 68bfaea224 (8 days ago), 2024-03-20 15:36:31 -0700
    • Engine revision a5c24f538d
    • Dart version 3.3.2
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/user/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/user/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] VS Code (version 1.87.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.84.0

[✓] Connected device (3 available)
    • iPhone (Alexander) (mobile) • 00000000-001231230212301E • ios            • iOS 17.3.1 21D61
    • macOS (desktop)             • macos                     • darwin-x64     • macOS 14.4.1 23E224 darwin-x64
    • Chrome (web)                • chrome                    • web-javascript • Google Chrome 123.0.6312.86

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Version of clang++

-> % clang++ --version Apple clang version 15.0.0 (clang-1500.3.9.4) Target: x86_64-apple-darwin23.4.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Additional context

No response

fzyzcjy commented 2 months ago

Yes, as you mentioned, I also guess it is not likely to be a bug of flutter_rust_bridge. Indeed it says:

dlopen failed: cannot locate symbol "__extenddftf2" referenced by "/data/app/~~Fd1yr5C6LdrkLyzJ--Bjbg==/com.example.frb_sqlite-TarkLSXer1ZxQ-Ec6U9CVA==/lib/x86_64/librust_lib_frb_sqlite.so"...

A quick search shows e.g. https://github.com/tauri-apps/tauri/issues/7413

And what about asking/searching in the sqlite lib you use (e.g. "it has blahblah error when on x86 android")

NightBlaze commented 2 months ago

Yep, I saw https://github.com/tauri-apps/tauri/issues/7413 and even found a workaround but didn't understand how to apply it for frb project https://github.com/rusqlite/rusqlite/issues/1380#issuecomment-1689765485

If you have android device I will be grateful if you approve that all works on real device.

fzyzcjy commented 2 months ago

but didn't understand how to apply it for frb project https://github.com/rusqlite/rusqlite/issues/1380#issuecomment-1689765485

Looks like you just need to fork libsqlite3-sys and modify its build.rs and done. (It seems not need to change anything in flutter_rust_bridge)

If you have android device I will be grateful if you approve that all works on real device.

I can confirm that flutter_rust_bridge works on real android pretty well; but never tried sqlite before in my own app.

malivix commented 2 months ago

Maybe my experience and how I fixed this issue will help you I was first have issue with "libc++_shared" is not found and after fixing it, I had same issue first I add build.rs to my rust project with this content

fn main() {
    #[cfg(target_os = "android")]
    println!("cargo:rustc-link-lib=c++_shared");
}

Second I copied libc++_shared.so files to jniLibs folder inside my rust_bridge project that is flutter project inside android, check picture

Screenshot 2024-03-30 at 15 59 40

what I was founded for cannot locate symbol.... issue it happen when you have duplicate lib or overwrited symbols maybe help you this approach

NightBlaze commented 2 months ago

Thanks for the hint. This solution works. Just need to create rust/build.rs and copy-paste the code. Don't forget to check env vars (they should exists).

fzyzcjy commented 2 weeks ago

Close since this is solved. Feel free to reopen if needed!

github-actions[bot] commented 3 days ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.