Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Unhandled Exception: Invalid argument(s): Failed to load dynamic library (dlopen failed: library "libsqlite3.so" not found) #420

Closed
joielechong opened this issue Mar 1, 2020 · 54 comments

Comments

@joielechong
Copy link

joielechong commented Mar 1, 2020

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: flutter/flutter#55827 (comment). 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.

@simolus3
Copy link
Owner

simolus3 commented Mar 1, 2020

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!

@joielechong
Copy link
Author

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

@simolus3
Copy link
Owner

simolus3 commented Mar 1, 2020

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?

@joielechong
Copy link
Author

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.

@joielechong
Copy link
Author

joielechong commented Mar 3, 2020

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.

@joielechong
Copy link
Author

I forgot to tell you that I'm using a prepopulated database like in #346 case.

I found a work around for this problem:

  • Use admob_flutter instead of firebase_admob.
  • Then, I need to initialized the admob until the apps has been relaunch more than 2 times.

Initializing the admob directly after initializing the moor database won't work.

Though I think there is something weird happened because of the admob :/

@simolus3
Copy link
Owner

simolus3 commented Mar 3, 2020

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?

@joielechong
Copy link
Author

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:

  • add admob dependencies
    firebase_admob: ^0.9.1+3
  • add admob test app id to AndroidManifest.xml
<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>
  • initialize admob in MyApp:
  @override
  void initState() {
    FirebaseAdMob.instance.initialize(appId: FirebaseAdMob.testAppId);
    bloc = TodoAppBloc();
    super.initState();
  }

@joielechong
Copy link
Author

joielechong commented Mar 5, 2020

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?

@joielechong
Copy link
Author

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.

@niusounds
Copy link

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")
    }
  }
}

@niusounds
Copy link

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.

@joielechong
Copy link
Author

joielechong commented Mar 27, 2020

@niusounds: That is a very good news! Does every error vanished when you're using moor_flutter?

@niusounds
Copy link

It seems moor_flutter works good.

@luccasmaso
Copy link

luccasmaso commented Apr 14, 2020

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; 
}

@joielechong
Copy link
Author

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

@novas1r1
Copy link

novas1r1 commented Apr 28, 2020

Hello,
I'm having the same problem with my tests on my PC (windows).

I am using:

  • moor: ^2.4.2
  • moor_generator: ^2.4.0
  • moor_ffi: ^0.4.0

[√] 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

@simolus3
Copy link
Owner

@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');
}

@novas1r1
Copy link

@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!

@knaeckeKami
Copy link
Contributor

knaeckeKami commented May 5, 2020

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)

@jesseframework
Copy link

Am having the same proble. I try using
void main() {
open.overrideFor(OperatingSystem.windows, _openOnWindows);
}

DynamicLibrary _openOnWindows() {
return DynamicLibrary.open(r'C:\Path\to\sqlite3.dll');
}

But did not work. Is there another working around. Where can i put the sqlite3 dll for this to work.

@simolus3
Copy link
Owner

simolus3 commented May 5, 2020

@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?

@jesseframework
Copy link

jesseframework commented May 5, 2020

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

@knaeckeKami
Copy link
Contributor

For now, it's just this one user with this one device.

@knaeckeKami
Copy link
Contributor

@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.

@simolus3
Copy link
Owner

simolus3 commented May 5, 2020

@knaeckeKami Can you reproduce flutter/flutter#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.

@knaeckeKami
Copy link
Contributor

knaeckeKami commented May 5, 2020

The device is a Samsung Galaxy S5. The projects both run without exceptions and all tests succeed.

@knaeckeKami
Copy link
Contributor

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.

@knaeckeKami
Copy link
Contributor

I still haven't received any new crashes with my workaround. I am pretty confident that it solves the problem.

@MisterFab1an
Copy link

MisterFab1an commented May 17, 2020

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.

@knaeckeKami
Copy link
Contributor

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:

  1. Some Android devices running older android versions/specific ABIs not loading dynamic libraries properly
  2. Issue [shared_preferences] Plugin project :shared_preferences_web not found. Please update settings.gradle. flutter/flutter#55827 (comment) in flutter which causes problems in the android build

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

@simolus3
Copy link
Owner

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.

@marwfair
Copy link

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.

@PiN73
Copy link
Contributor

PiN73 commented Jun 12, 2020

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'));

@simolus3
Copy link
Owner

As @knaeckeKami said, there were two problems here:

  • a problem with Flutter's gradle setup, which means that native libraries aren't available. I'll add the settings.gradle workaround to the documentation website.
  • a problem with the loader on some Android versions, for which moor_ffi: ^0.6.0 contains a builtin workaround

I'll close this issue since it sounds like both problems are fixed now.

@EmmanuelMess
Copy link

EmmanuelMess commented Jun 18, 2020

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

@simolus3
Copy link
Owner

@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
Copy link

EmmanuelMess commented Jun 18, 2020

@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.

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).

@simolus3
Copy link
Owner

The libsqlite3-dev package works on my Ubuntu server, maybe try that one?

@EmmanuelMess
Copy link

The libsqlite3-dev package works on my Ubuntu server, maybe try that one?

That worked thanks!

@simolus3
Copy link
Owner

simolus3 commented Jul 3, 2020

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.

@apoleo88
Copy link

apoleo88 commented Jul 7, 2020

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

@simolus3
Copy link
Owner

simolus3 commented Jul 8, 2020

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.

@apoleo88
Copy link

apoleo88 commented Jul 8, 2020

Yes, now it works!

@TaifRaoof
Copy link

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>

@kankadev
Copy link

kankadev commented Dec 9, 2021

Have the same problem for windows target, android works. The suggested dependency overwriting is not a solution for me, because I don't use the moor dependency since it called drift now.

@knaeckeKami
Copy link
Contributor

you probably need to include the sqlite.dll for windows.
see https://drift.simonbinder.eu/docs/platforms/#windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests