Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.DllNotFoundException: /system/lib/libsqlite.so- Xamarin Forms

I am facing SQLite issue in xamarin forms project. However SQLiteConnection I am trying to instantiating in Android platform there getting exception.

Libraries using in android & iOS project

  • SQLite.Net-PCL 3.1.1
  • SQLite.Net.Core-PCL 3.1.1

menifest file target versions

<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" />

AndroidSQLite.cs file

using SQLite;
using SQLite.Net;
using System.IO;
namespace XamarinTestList.Droid.Implementations
{
public class AndroidSQLite : ISQLite
{
    public SQLiteConnection GetConnection()
    {
        string documentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var path = Path.Combine(documentPath, DatabaseHelper.DbFileName); //DbFileName="Contacts.db"
        var plat = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
        string dpPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "user.db3");
        var conn= new SQLiteConnection(plat, dpPath);
        return conn;
    }
}
}

Getting exception at

 var conn= new SQLiteConnection(plat, dpPath);

I have installed above libraries on solution level but in shared project, there is no option available to install Nuget packages. Shared project screen shot

enter image description here

Following this article to learn SQLite with xamarin xamarin forms-mvvm-sqlite-sample.

Full exception below

{System.DllNotFoundException: /system/lib/libsqlite.so at (wrapper managed-to-native) SQLite.Net.Platform.XamarinAndroid.SQLiteApiAndroidInternal.sqlite3_open_v2(byte[],intptr&,int,intptr) at SQLite.Net.Platform.XamarinAndroid.SQLiteApiAndroid.Open (System.Byte[] filename, SQLite.Net.Interop.IDbHandle& db, System.Int32 flags, System.IntPtr zvfs) [0x00000] in <8dbf6ff85082469fb9d4dfaa9eae6b69>:0 at SQLite.Net.SQLiteConnection..ctor (SQLite.Net.Interop.ISQLitePlatform sqlitePlatform, System.String databasePath, SQLite.Net.Interop.SQLiteOpenFlags openFlags, System.Boolean storeDateTimeAsTicks, SQLite.Net.IBlobSerializer serializer, System.Collections.Generic.IDictionary2[TKey,TValue] tableMappings, System.Collections.Generic.IDictionary2[TKey,TValue] extraTypeMappings, SQLite.Net.IContractResolver resolver) [0x000a2] in <8f2bb39aeff94a30a8628064be9c7efe>:0 at SQLite.Net.SQLiteConnection..ctor (SQLite.Net.Interop.ISQLitePlatform sqlitePlatform, System.String databasePath, System.Boolean storeDateTimeAsTicks, SQLite.Net.IBlobSerializer serializer, System.Collections.Generic.IDictionary2[TKey,TValue] tableMappings, System.Collections.Generic.IDictionary2[TKey,TValue] extraTypeMappings, SQLite.Net.IContractResolver resolver) [0x00000] in <8f2bb39aeff94a30a8628064be9c7efe>:0

like image 986
R15 Avatar asked Aug 28 '18 07:08

R15


2 Answers

The SQLite.Net-PCL nuget you used is no longer being maintained by it's author. You need to switch to other similar package sqlite-net-pcl

First remove the existing SQLite.Net-PCL nuget from all projects and install the one I mentioned above. Remove all old SQLite related references from all files.

You need to make some changes in SQLite connection related dependency service classes. Then you may adjust some other code which relates to SQLite. Resolve the references by adding appropriate using statements for the new nuget package.

I got the same error and I solved it by switching to above nuget package.

NOTE : The new nuget package does not have InsertOrReplaceAll method yet. So If you have used this method from previous nuget, then for the new nuget, you need to create an InsertOrReplaceAll extension method.

EDIT: To get the SQLiteConnection:

public SQLiteConnection GetConnection()
{
    return new SQLiteConnection(dpPath, false);
}

There is no need to pass the platform in this new nuget. Pass false if you don't want to store DateTime as Ticks.

like image 90
MilanG Avatar answered Sep 25 '22 14:09

MilanG


2 choices

  1. Change targetSdkVersion < 24, since Android 7, app can not access system private libraries.
  2. Add libsqlite.so into your own lib folder.
like image 20
shingo Avatar answered Sep 22 '22 14:09

shingo