Edited by @simolus3 for visibility: If you encounter this problem, please check that you're using moor_ffi version 0.6.0 or later. There's a Flutter bug causing builds to not include native libraries: https://github.com/flutter/flutter/issues/55827#issuecomment-623779910. Please follow the workaround described in the linked comment.
First, thank you very much for this wonderful project!
I've get a problem. After upgrading Flutter and run my Flutter with moor based project, I've get the following error:
E/flutter (21278): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library (dlopen failed: library "libsqlite3.so" not found)
E/flutter (21278): #0 _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:13:55)
E/flutter (21278): #1 new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:22:12)
E/flutter (21278): #2 _defaultOpen (package:moor_ffi/src/load_library.dart:24:27)
E/flutter (21278): #3 OpenDynamicLibrary.openSqlite (package:moor_ffi/src/load_library.dart:78:12)
E/flutter (21278): #4 new _SQLiteBindings (package:moor_ffi/src/bindings/bindings.dart:97:19)
E/flutter (21278): #5 bindings (package:moor_ffi/src/bindings/bindings.dart:197:53)
E/flutter (21278): #6 new Database.open (package:moor_ffi/src/impl/database.dart:40:9)
E/flutter (21278): #7 new Database.openFile (package:moor_ffi/src/impl/database.dart:29:52)
E/flutter (21278): #8 _VmDelegate.open (package:moor_ffi/src/vm_database.dart:39:22)
E/flutter (21278): #9 DelegatedDatabase.ensureOpen.<anonymous closure> (package:moor/src/runtime/executor/helpers/engines.dart:246:22)
E/flutter (21278): <asynchronous suspension>
E/flutter (21278): #10 DelegatedDatabase.ensureOpen.<anonymous closure> (package:moor/src/runtime/executor/helpers/engines.dart)
E/flutter (21278): #11 BasicLock.synchronized (package:synchronized/src/basic_lock.dart:32:26)
E/flutter (21278): #12 DelegatedDatabase.ensureOpen (package:moor/src/runtime/executor/helpers/engines.dart:238:25)
E/flutter (21278): #13 LazyDatabase.ensureOpen.<anonymous closure> (package:moor/src/utils/lazy_database.dart:50:49)
E/flutter (21278): #14 _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter (21278): #15 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (21278): #16 _FutureListener.handleValue (dart:async/future_impl.dart:140:18)
E/flutter (21278): #17 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
E/flutter (21278): #18 Future._propagateToListeners (dart:async/future_impl.dart:711:32)
E/flutter (21278): #19 Future._completeWithValue (dart:async/future_impl.dart:526:5)
E/flutter (21278): #20 Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:556:7)
E/flutter (21278): #21 _rootRun (dart:async/zone.dart:1126:13)
E/flutter (21278): #22 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (21278): #23 _CustomZone.runGuarded (dart:async/zone.dart:925:7)
E/flutter (21278): #24 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
E/flutter (21278): #25 _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
E/flutter (21278): #26 _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
E/flutter (21278):
I'm using the following moor dependencies:
moor: ^2.4.1
moor_ffi: ^0.4.0
Here my flutter doctor result:
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, v1.15.4-pre.235, on Linux, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Linux toolchain - develop for Linux desktop
[!] Android Studio (version 3.5)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] IntelliJ IDEA Community Edition (version 2019.3)
[✓] Connected device (2 available)
! Doctor found issues in 1 category.
Is this related to flutter version? If yes, what is the correct flutter version to temporarily fixing the problem?
Thank you.
It shouldn't be related to the Flutter version. Can you please tell me which operating system and (os) version you're running your app on? Thanks!
I'm running the flutter app in Linux Slackware64 14.2 and Android version 6.0.1. It working fine before updating the flutter while using the following moor dependencies:
moor: ^2.1.0
moor_ffi: ^0.2.0
Oh, that's interesting. Can you downgrade to moor_ffi: ^0.3.0 and see if that works? If it does, can you please share the abi of the Android device?
Downgrading to moor_ffi: ^0.3.0 or moor_ffi: ^0.2.0 doesn't work too.
But building and running moor_shared example is ok.
There is something wrong with my project that seems doesn't related to moor. I'll try migrating and rebuilding the project from scratch and will inform you later.
Thank you.
OK, I've found the source of the problem. It's because I'm using firebase_admob dependency. I'm calling the following line after initializing database:
FirebaseAdMob.instance.initialize();
It seems that it remove all the moor related folder in application document path so that the moor can't work anymore. Though this is only my wild guess right now.
No problem with moor when removing the offending line.
I forgot to tell you that I'm using a prepopulated database like in https://github.com/simolus3/moor/issues/346 case.
I found a work around for this problem:
Initializing the admob directly after initializing the moor database won't work.
Though I think there is something weird happened because of the admob :/
On Android, moor_ffi bundles a native sqlite3.so library with your application. It is very, very weird that admob could interfere with that.
I'll take a good look at that when I have time, but it seems like it will be tough to track down and fix. Does this happen with a mostly-empty project that only uses moor_ffi and admob?
I've tested it with moor_shared example in the moor project. The problem is consistent. Tested on Vivo 1610 device, Android 6.0.1, armeabi-v7a,armeabi.
The problem can easily reproduced with the following steps:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.moor_shared">
...
<application
android:name="io.flutter.app.FlutterApplication"
android:label="moor_shared"
android:icon="@mipmap/ic_launcher">
<activity
...
</activity>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
</application>
</manifest>
@override
void initState() {
FirebaseAdMob.instance.initialize(appId: FirebaseAdMob.testAppId);
bloc = TodoAppBloc();
super.initState();
}
After a couple times running my apps with admob_flutter dependency, I found that sometimes that the error:
E/flutter (21278): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library (dlopen failed: library "libsqlite3.so" not found)
still happens. Looks like moor_ffi is killed by the admob. Killing the app and relaunch will fix the problem.
Is this something related with the MethodChannel used by moor_ffi and admob_flutter?
I've found that the moor_ffi coupled with admob_flutter only stopped when the app is hot reloaded. It works fine when restarting the app manually. But the problem still persisted when moor_ffi coupled with firebase_admob.
I have same issue in my project. My device is Nexus 4 Android 5.0 armeabi-v7a.
I tested with simple new project which only depends with moor. It works well. But it fails in production project. I don't find cause yet.
Curious to say, this looks like to fix it but I don't know why.
class MainActivity: Activity() { // Entry point Activity of my app
// ...
companion object {
init {
System.loadLibrary("sqlite3")
}
}
}
I found that some 64bit devices also have same problem. Probably it is Dart FFI's bug.
I migrated from moor_ffi to moor_flutter to prevent this error in my project.
@niusounds: That is a very good news! Does every error vanished when you're using moor_flutter?
It seems moor_flutter works good.
Same problem here the build target is for Android
I/flutter (22934): Invalid argument(s): Failed to load dynamic library (dlopen failed: library "libsqlite3.so" not found)
I/flutter (22934): Invalid argument(s): Failed to load dynamic library (dlopen failed: library "libsqlite3.so" not found)
moor: ^2.4.2
moor_ffi: ^0.4.0
class Database extends _$Database {
Database() : super(VmDatabase(File('app.db'), logStatements: true));
@override
int get schemaVersion => 1;
}
I can confirm that the problem is fixed when using moor_flutter at least on my test devices:
Vivo 1610 device, Android 6.0.1, armeabi-v7a,armeabi,
LG Q6, Android 8, armebi-v8a
Sony Xperia Z3 Compact
Hello,
I'm having the same problem with my tests on my PC (windows).
I am using:
[√] Flutter (Channel stable, v1.12.13+hotfix.9, on Microsoft Windows [Version 10.0.18363.778], locale
de-DE)
• Flutter version 1.12.13+hotfix.9 at C:\Anwendungen\flutter
• Framework revision f139b11009 (4 weeks ago), 2020-03-30 13:57:30 -0700
• Engine revision af51afceb8
• Dart version 2.7.2
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at C:\Anwendungen\AndroidSDK
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-29, build-tools 29.0.2
• ANDROID_HOME = C:\Anwendungen\AndroidSDK
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
• All Android licenses accepted.
[√] Android Studio (version 3.6)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 45.1.1
• Dart plugin version 192.7761
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
[√] Connected device (1 available)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 10 (API 29) (emulator)
This is the error message on windows:
ERROR: Invalid argument(s): Failed to load dynamic library (126)
dart:ffi new DynamicLibrary.open
package:moor_ffi/src/load_library.dart 34:27 _defaultOpen
package:moor_ffi/src/load_library.dart 80:12 OpenDynamicLibrary.openSqlite
package:moor_ffi/src/bindings/bindings.dart 92:19 new _SQLiteBindings
package:moor_ffi/src/bindings/bindings.dart 184:53 bindings
package:moor_ffi/src/impl/database.dart 40:9 new Database.open
package:moor_ffi/src/impl/database.dart 32:41 new Database.memory
package:moor_ffi/src/vm_database.dart 41:22 _VmDelegate.open
package:moor/src/runtime/executor/helpers/engines.dart 246:22 DelegatedDatabase.ensureOpen.<fn>
I already downloaded sqlite3.dll and added it to the path. Didn't change anything.
Is there something I missed?
Best regards
Verena
@novas1r1 I don't have a lot of experience with Windows development. moor_ffi should essentially call LoadLibraryW("sqlite3.dll"), so I'm not sure why that wouldn't work if sqlite3.dll is in your path.
Does it work if you provide the absolute path manually?
import 'dart:ffi';
import 'package:moor_ffi/open_helper.dart';
void main() {
open.overrideFor(OperatingSystem.windows, _openOnWindows);
}
DynamicLibrary _openOnWindows() {
return DynamicLibrary.open(r'C:\Path\to\sqlite3.dll');
}
@simolus3 fixed it this morning by adding the whole sqlite3 tool-bundle from "Precompiled Binaries for Windows" in the path. sqlite3.dll wasn't enough for me. Thanks anyway for the alternate solution!
I'm getting crash reports from one user.
For now, it's only one user with a Sony Xperia Z3 Dual on Android 6.0.
I shipped an update with Moor 3.0.0 today, so I suspect it's related to that. (The crash happened with Moor 3.0.0 and moor_ffi 0.5.0, but I cannot rule out that this crash would have happened on previous versions too, it might be a new user that just installed the app)
Stacktrace:
Non-fatal Exception: "Invalid argument(s): Failed to load dynamic library (dlopen failed: library \"libsqlite3.so\" not found)"
at ._open(.java:13)
at new DynamicLibrary.open(new DynamicLibrary.java:22)
at ._defaultOpen(.java:20)
at OpenDynamicLibrary.openSqlite(OpenDynamicLibrary.java:80)
at .new _SQLiteBindings(.java:121)
at .bindings(.java:262)
at new Database.open(new Database.java:52)
at new Database.openFile(new Database.java:37)
at _VmDelegate.open(_VmDelegate.java:39)
at DelegatedDatabase.ensureOpen.<fn>(ensureOpen.java:244)
at BasicLock.synchronized(BasicLock.java:34)
at _MoorServer._handleEnsureOpen(_MoorServer.java:74)
at IsolateCommunication.setRequestHandler.<fn>.<fn>(<fn>.java:167)
at .<asynchronous suspension>(.java)
Am having the same proble. I try using
void main() {
open.overrideFor(OperatingSystem.windows, _openOnWindows);
}
DynamicLibrary _openOnWindows() {
return DynamicLibrary.open(r'C:\Path\tosqlite3.dll');
}
But did not work. Is there another working around. Where can i put the sqlite3 dll for this to work.
@jesseframework On Windows you can also put sqlite3.dll in a folder that's in your PATH and then use the default open function (e.g. not use overrideFor at all).
@knaeckeKami moor_ffi builds the same from 0.4.0 to 0.5.0, so I assume this would have happened on an older version as well. Did you see similar errors on other armv7a devices or on devices with another abi?
In my first comment i did this:
On Windows you can also put sqlite3.dll in a folder that's in your PATH and then use the default open function (e.g. not use overrideFor at all).
Using this:
void main() {
open.overrideFor(OperatingSystem.windows, _openOnWindows);
}
DynamicLibrary _openOnWindows() {
return DynamicLibrary.open(r'C:\Path\to\sqlite3.dll');
}
Is there another way to do it? what do you meant by your PATH?
Am using moor_ffi:
moor: ^2.4.2
moor_ffi: ^0.4.0
For now, it's just this one user with this one device.
@simolus3 I can now reproduce this error locally on an old device that I had.
If you need any more info, I'm glad to help.
@knaeckeKami Can you reproduce https://github.com/flutter/flutter/issues/54948 on that device? That's a similar problem we were seeing on old arm devices as well.
If that minimal repro doesn't show any prolems, can you try to run moor's integration tests?
git clone https://github.com/simolus3/moor.git
cd moor/extras/integration_tests/flutter_db/
flutter packages get
flutter run --release -t lib/moor_ffi.dart
@jesseframework If you put the downloaded sqlite3.dll into a file that's accessible from the Windows PATH variable, it should work without using open.overrideFor.
The device is a Samsung Galaxy S5. The projects both run without exceptions and all tests succeed.
Interesting.
I found this thread: https://groups.google.com/forum/#!topic/android-ndk/OJxg_550eHE
And I thought I'll try calling DynamicLibrary.open with an absolute path.
So I tried
open.overrideFor(OperatingSystem.android, safeSqliteOpen);
with
DynamicLibrary safeSqliteOpen() {
DynamicLibrary libsqlite;
try {
libsqlite = DynamicLibrary.open('libsqlite3.so');
} catch (e) {
print("fail, fallback 1");
libsqlite = DynamicLibrary.open(
"/data/data/<my app id>/lib/libsqlite3.so");
print('fallback success');
}
return libsqlite;
}
but since I'm using the Isolates API, this doesn't work (the Isolate will have a separate Instance of the "open" object).
So I forked moor_ffi and changed the _defaultOpen() method:
DynamicLibrary _defaultOpen() {
if (Platform.isLinux || Platform.isAndroid) {
DynamicLibrary libsqlite;
try {
libsqlite = DynamicLibrary.open('libsqlite3.so');
} catch (e) {
print("fail, fallback 1");
libsqlite = DynamicLibrary.open(
"/data/data/<my app id>/lib/libsqlite3.so");
print('fallback success');
}
return libsqlite;
...
}
And it worked!
It seems on old Android versions (at least some), dlopen() must be called with an absolute path.
Huh, so it's probably another dependency that's causing this. If you run a release build (e.g. flutter build apk --release -t lib/moor_ffi.dart) for both the integration tests and your app, do you see any difference in the native libs? Do both apks contain libsqlite3.so on armv7?
This is the folder structure under lib/ of the release build of moor_ffi:
ls -l
total 0
drwxr-xr-x 5 kami staff 160 May 5 21:14 arm64-v8a
drwxr-xr-x 5 kami staff 160 May 5 21:14 armeabi-v7a
drwxr-xr-x 3 kami staff 96 May 5 21:14 x86
drwxr-xr-x 5 kami staff 160 May 5 21:14 x86_64
All ABIs contain libsqlite3.so.
This is the folder structure of my app:
ls -al
total 0
drwxr-xr-x 7 kami staff 224 May 5 21:23 .
drwxr-xr-x 42 kami staff 1344 May 5 21:23 ..
drwxr-xr-x 6 kami staff 192 May 5 21:23 arm64-v8a
drwxr-xr-x 3 kami staff 96 May 5 21:23 armeabi
drwxr-xr-x 6 kami staff 192 May 5 21:23 armeabi-v7a
drwxr-xr-x 4 kami staff 128 May 5 21:23 x86
drwxr-xr-x 6 kami staff 192 May 5 21:23 x86_64
There is an extra armeabi folder!
This are the contents:
ls ./*
./arm64-v8a:
libapp.so libcrashlytics.so libflutter.so libsqlite3.so
./armeabi:
libcrashlytics.so
./armeabi-v7a:
libapp.so libcrashlytics.so libflutter.so libsqlite3.so
./x86:
libcrashlytics.so libsqlite3.so
./x86_64:
libapp.so libcrashlytics.so libflutter.so libsqlite3.so
The armeabi does not have libsqlite.so, this may be the culprit!
Anyway, this should still not happen AFAIK.
adb shell getprop | grep abi
[camera2.portability.force_api]: [1]
[ro.product.cpu.abi]: [armeabi-v7a]
[ro.product.cpu.abi2]: [armeabi]
[ro.product.cpu.abilist]: [armeabi-v7a,armeabi]
[ro.product.cpu.abilist32]: [armeabi-v7a,armeabi]
[ro.product.cpu.abilist64]: []
The primary ABI of this device is armeabi-v7a, it should look there first!
But I don't know much about the behavior of dlopen() in Android, this might be expected.
We commented at nearly the same moment, be sure to check out my other comment in case you missed it.
Edit: Even with Crashlytics removed, the armeabi folder is gone:
./arm64-v8a:
libsqlite3.so
./armeabi-v7a:
libapp.so libflutter.so libsqlite3.so
./x86:
libsqlite3.so
./x86_64:
libsqlite3.so
but the error still remains... (except when I use the full-path fallback).
but since I'm using the Isolates API, this doesn't work (the Isolate will have a separate Instance of the "open" object).
You could also set the open callback from a method that will run on the background isolate:
DatabaseConnection _openForBackgroundIsolate() {
open.overrideFor(OperatingSystem.android, safeSqliteOpen);
final db = LazyDatabase(() { /*...*/ });
return DatabaseConnection.fromExecutor(db);
}
If this reliably solves the problem, I'll look for ways to integrate this to moor. It will be a bit tricky since moor_ffi is a pure-Dart package that can't depend on Flutter. So I'm not sure how we'll get the application id when loading the library. Parsing /proc/self/maps for this sounds horrible, but if it works ¯\_(ツ)_/¯
You could also set the open callback from a method that will run on the background isolate:
Ah yeah, didn't think of that!
I have no idea if it solves the problem reliably. It seems to affect only a tiny percent of my userbase and I only have one device to reproduce it. It does, however, solve it on this device, so I'm optimistic.
I'm pretty sure there's something else that we're missing, I don't understand why the ffi integration test works but my app doesn't.
But I now have a solution that works well for my device and could be integrated into moor_ffi without dependency for flutter:
// https://github.com/simolus3/moor/issues/420
DynamicLibrary safeSqliteOpen() {
DynamicLibrary libsqlite;
try {
libsqlite = DynamicLibrary.open('libsqlite3.so');
} catch (e) {
//fallback for devices that cannot load dynamic libraries by name: load the library with an absolute path
//read the app id
var appid = File("/proc/self/cmdline").readAsStringSync();
// the file /proc/self/cmdline returns a string with many trailing \0 characters, which makes the string pretty useless for dart, many
// operations will not work correctly. remove these trailing zero bytes.
appid = String.fromCharCodes(appid.codeUnits.where((element) => element != 0));
final loadPath = "/data/data/$appid/lib/libsqlite3.so";
libsqlite = DynamicLibrary.open(loadPath);
}
return libsqlite;
}
I don't know if that's as reliable as parsing /proc/self/maps, but it's far easier.
I now have some more information on affected devices (According to crash reports of my users):
It happens on:
All affected devices run Android 6.0.1
Thanks! I'll add the /proc/self/cmdline hack to the default open strategy of moor_ffi. Have you released an update for that yourself or do you know whether it fixes the problem on those devices?
I released an update with the /proc/self/cmdline hack and have not yet received crash reports for the new version.
However, I'm not yet confident that it really fixes the problem for all devices, the affected users might not have upgraded yet or simply stopped using the app.
I still haven't received any new crashes with my workaround. I am pretty confident that it solves the problem.
So I've come across the same problem:
[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library (dlopen failed: library "libsqlite3.so" not found)
and tried @knaeckeKami and @simolus3 workaround with overwriting the open callback with the safeSqliteOpen method.
However, this didn't help - after executing it that way I got the error:
[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library (dlopen failed: library "/data/data/com.example.moorsample/lib/libsqlite3.so" not found) with the new path to the sqlite3 library.
After a bit of investigating in the build process I noticed that I got this warning
Plugin project :moor_ffi not found. Please update settings.gradle. which led me to this flutter issue.
The problem with my build was the automatically created settings.gradle which was not working properly even though it was auto-generated by flutter.
Solution
Changing settings.gradle as the issue comment suggests fixed all the errors.
Plugin project :moor_ffi not found. Please update settings.gradle. didn't appear anymore and the sqlite3 library could now be loaded/found without overwriting the open callback.
(Caution: After the changes the app has to be rebuilt, Hot Restart does not solve the errors)
I hope this helps.
TLDR
Solution for me was changing the settings.gradle according to this and not overwriting the open callback.
I think that's a regression introduced in flutter 1.18, which leads to the .so file not being included in the apk at all.
So there might be two causes for this exception:
My project uses flutter 1.17 stable, which still has the working settings.gradle, and I need the workaround in order to support some old android devices
I've just released moor_ffi version 0.6.0 which should fix the first problem (thanks again to @knaeckeKami for the investigation and the workaround!).
I'll keep this open for visibility, but it looks like the remaining failures aren't anything moor could fix.
Changing the moor_ffi version to 0.6.0 fixed the initial problem for me for Flutter apps, but I also have a flutter module that I add to a native Android app.
If I'm on the flutter master channel and run flutter build aar from within my module project, I get Plugin project :moor_ffi not found. Please update settings.gradle.. If I downgrade moor_ffi to 0.5.0 and run flutter build aar again, it generates the aar files. So when I add that aar to my android project and run it, I get the Invalid argument(s): Failed to load dynamic library (dlopen failed: library "libsqlite3.so" not found) error. I tried updating my settings.gradle file and downgrading my flutter version and doing different combinations of things, but keep getting errors.
This was working a couple of months ago though.
Same for moor_ffi 0.6.0
Launching lib\main.dart on Android SDK built for x86 in debug mode...
Running Gradle task 'assembleDebug'...
Plugin project :moor_ffi not found. Please update settings.gradle.
then apk is built and installed but using database causes Flutter error at runtime:
Invalid argument(s): Failed to load dynamic library (dlopen failed: library "/data/data/myapp/lib/libsqlite3.so" not found)
Flutter doctor
C:\Users\Pavel>flutter doctor -v
[√] Flutter (Channel stable, v1.17.3, on Microsoft Windows [Version 10.0.18363.900], locale ru-RU)
• Flutter version 1.17.3 at C:\flutterSdk\flutter
• Framework revision b041144f83 (8 days ago), 2020-06-04 09:26:11 -0700
• Engine revision ee76268252
• Dart version 2.8.4
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
• Android SDK at C:\Users\Pavel\AppData\Local\Android\sdk
• Platform android-29, build-tools 29.0.3
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted.
[√] Android Studio (version 4.0)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 46.0.2
• Dart plugin version 193.7361
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
[√] Connected device (1 available)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 10 (API 29) (emulator)
• No issues found!
Manually editing settings.gradle OR using moor_flutter instead of moor_ffi works
import 'package:moor_flutter/moor_flutter.dart';
...
MyDatabase() : super(FlutterQueryExecutor.inDatabaseFolder(path: 'db.sqlite'));
As @knaeckeKami said, there were two problems here:
settings.gradle workaround to the documentation website.moor_ffi: ^0.6.0 contains a builtin workaroundI'll close this issue since it sounds like both problems are fixed now.
Using moor_ffi: ^0.6.0 and given setting.gradle.
Getting SqfliteFfiException(error, Invalid argument(s): Failed to load dynamic library (libsqlite3.so: cannot open shared object file: No such file or directory)} DatabaseException(Invalid argument(s): Failed to load dynamic library (libsqlite3.so: cannot open shared object file: No such file or directory)) {} when running tests.
Test:
void main() {
AppDatabase database;
DeadlineDao deadlineDao;
setUp(() async {
database = await $FloorAppDatabase
.inMemoryDatabaseBuilder()
.build();
deadlineDao = database.deadlineDao;
});
tearDown(() async {
await database.close();
});
test('', () {});
}
The database works normally, but when running the test I get:
Testing started at 15:57 ...
/.../flutter/bin/flutter --no-color test --machine test/widget_test.dart
package:sqflite_common_ffi/src/isolate.dart 39:9 SqfliteIsolate.handle
NoSuchMethodError: The method 'close' was called on null.
Receiver: null
Tried calling: close()
dart:core Object.noSuchMethod
test/widget_test.dart 20:20 main.<fn>
===== asynchronous gap ===========================
dart:async _AsyncAwaitCompleter.completeError
test/widget_test.dart main.<fn>
SqfliteFfiException(error, Invalid argument(s): Failed to load dynamic library (libsqlite3.so: cannot open shared object file: No such file or directory)} DatabaseException(Invalid argument(s): Failed to load dynamic library (libsqlite3.so: cannot open shared object file: No such file or directory)) {}
libsqlite3-0 is installed.
Flutter: channel dev
OS: Ubuntu 20.04
@EmmanuelMess so you have a /usr/lib/libsqlite3.so or /lib/libsqlite3.so file on your machine? The path needs to match exactly, if there's a .0 suffix then dlopen won't find it.
@EmmanuelMess so you have a
/usr/lib/libsqlite3.soor/lib/libsqlite3.sofile on your machine? The path needs to match exactly, if there's a.0suffix thendlopenwon't find it.
No, I do not. But sudo apt install libsqlite3-0 says "libsqlite3-0 ya está en su versión más reciente (3.31.1-4ubuntu0.1)." (libsqlite3 package doesn't exist).
The libsqlite3-dev package works on my Ubuntu server, maybe try that one?
The
libsqlite3-devpackage works on my Ubuntu server, maybe try that one?
That worked thanks!
If you were running into the issue where libsqlite3.so wasn't included in your apk, you can also try to add this to your pubspec:
dependency_overrides:
moor_ffi:
git:
url: https://github.com/simolus3/moor.git
ref: develop
path: moor_ffi
That version should work with the gradle files generated by the latest stable Flutter version as well. Please let me know if that doesn't fix the problem.
I have a moor project working fine, but today I created a new one and I got the same error when it tries to create the db.
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library (dlopen failed: library "/data/data/it.apoleo.utransfer/lib/libsqlite3.so" not found)
#0 _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:13:55)
#1 new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:22:12)
#2 _defaultOpen (package:moor_ffi/src/load_library.dart:41:31)
#3 OpenDynamicLibrary.openSqlite (package:moor_ffi/src/load_library.dart:99:12)
#4 new _SQLiteBindings (package:moor_ffi/src/bindings/bindings.dart:122:19)
#5 bindings (package:moor_ffi/src/bindings/bindings.dart:266:53)
#6 new Database.open (package:moor_ffi/src/impl/database.dart:52:9)
#7 new Database.openFile (package:moor_ffi/src/impl/database.dart:37:52)
#8 _VmDelegate.open (package:moor_ffi/src/vm_database.dart:39:22)
#9 DelegatedDatabase.ensureOpen.<anonymous closure> (package:moor/src/runtime/executor/helpers/engines.dart:244:22)
<asynchronous suspension>
#10 DelegatedDatabase.ensureOpen.<anonymous closure> (package:moor/src/runtime/executor/helpers/engines.dart)
#11 BasicLock.synchronized (package:synchronized/src/basic_lock.dart:32:26)
#12 DelegatedDatabase.ensureOpen (package:moor/src/runtime/executor/helpers/engines.dart:238:25)
#13 LazyDatabase.ensureOpen.<anonymous closure> (package:moor/src/utils/lazy_database.dart:43:49)
#14 _rootRunUnary (dart:async/zone.dart:1192:38)
#15 _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#16 _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#17 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#18 Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#19 Future._completeWithValue (dart:async/future_impl.dart:526:5)
#20 Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:556:7)
#21 _rootRun (dart:async/zone.dart:1184:13)
#22 _CustomZone.run (dart:async/zone.dart:1077:19)
#23 _CustomZone.runGuarded (dart:async/zone.dart:979:7)
#24 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1019:23)
#25 _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
#26 _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
I am using:
dependency_overrides:
moor_generator:
git:
url: https://github.com/simolus3/moor.git
ref: beta
path: moor_generator
sqlparser:
git:
url: https://github.com/simolus3/moor.git
ref: beta
path: sqlparser
moor_ffi:
git:
url: https://github.com/simolus3/moor.git
ref: beta
path: moor_ffi
Argh, sorry - it should be the develop branch for moor_ffi:
dependency_overrides:
moor_ffi:
git:
url: https://github.com/simolus3/moor.git
ref: develop
path: moor_ffi
Let me know if that works for you.
Yes, now it works!
this error occur to my also in mocOS pc and windows pc when create and run new project on android device and emulator
flutter_app_demo3/lib/libsqlite3.so" not found)
E/flutter ( 7600): #0 _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:11:55)
E/flutter ( 7600): #1 new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:20:12)
E/flutter ( 7600): #2 _defaultOpen (package:sqlite3/src/load_library.dart:40:31)
E/flutter ( 7600): #3 OpenDynamicLibrary.openSqlite (package:sqlite3/src/load_library.dart:110:12)
E/flutter ( 7600): #4 sqlite3 (package:sqlite3/src/api/sqlite3.dart:9:34)
E/flutter ( 7600): #5 sqlite3 (package:sqlite3/src/api/sqlite3.dart)
E/flutter ( 7600): #6 _VmDelegate._createDatabase (package:moor/src/ffi/vm_database.dart:159:13)
E/flutter ( 7600): #7 _VmDelegate.open (package:moor/src/ffi/vm_database.dart:138:7)
E/flutter ( 7600): #8 DelegatedDatabase.ensureOpen.<anonymous closure> (package:moor/src/runtime/executor/helpers/engines.dart:253:22)
E/flutter ( 7600): <asynchronous suspension>
E/flutter ( 7600): #9 Lock.synchronized.callBlockAndComplete (package:moor/src/utils/synchronized.dart:18:16)
E/flutter ( 7600): <asynchronous suspension>
E/flutter ( 7600): #10 DatabaseConnectionUser.doWhenOpened.<anonymous closure> (package:moor/src/runtime/api/connection_user.dart)
E/flutter ( 7600): <asynchronous suspension>
Most helpful comment
Ah yeah, didn't think of that!
I have no idea if it solves the problem reliably. It seems to affect only a tiny percent of my userbase and I only have one device to reproduce it. It does, however, solve it on this device, so I'm optimistic.
I'm pretty sure there's something else that we're missing, I don't understand why the ffi integration test works but my app doesn't.
But I now have a solution that works well for my device and could be integrated into moor_ffi without dependency for flutter:
I don't know if that's as reliable as parsing /proc/self/maps, but it's far easier.