【问题标题】:System.DllNotFoundException: /system/lib/libsqlite.so- Xamarin FormsSystem.DllNotFoundException:/system/lib/libsqlite.so- Xamarin Forms
【发布时间】:2019-02-02 18:40:26
【问题描述】:

我在 xamarin 表单项目中遇到 SQLite 问题。但是SQLiteConnection我正在尝试在Android平台上实例化出现异常。

android & iOS 项目中使用的库

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

清单文件目标版本

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

AndroidSQLite.cs 文件

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

获取异常

 var conn= new SQLiteConnection(plat, dpPath);

我已经在解决方案级别安装了上述库,但在共享项目中,没有安装 Nuget 包的选项。共享项目屏幕截图

按照本文学习使用 xamarin 的 SQLite xamarin forms-mvvm-sqlite-sample.

下面的完全例外

{System.DllNotFoundException: /system/lib/libsqlite.so at (wrapper 托管到本地) SQLite.Net.Platform.XamarinAndroid.SQLiteApiAndroidInternal.sqlite3_open_v2(byte[],intptr&,int,intptr) 在 SQLite.Net.Platform.XamarinAndroid.SQLiteApiAndroid.Open (System.Byte[] 文件名,SQLite.Net.Interop.IDbHandle& db, System.Int32 标志,System.IntPtr zvfs) [0x00000] 在 :0 在 SQLite.Net.SQLiteConnection..ctor (SQLite.Net.Interop.ISQLitePlatform sqlitePlatform,System.String 数据库路径, SQLite.Net.Interop.SQLiteOpenFlags openFlags, System.Boolean storeDateTimeAsTicks,SQLite.Net.IBlobSerializer 序列化器, System.Collections.Generic.IDictionary2[TKey,TValue] tableMappings, System.Collections.Generic.IDictionary2[TKey,TValue] extraTypeMappings,SQLite.Net.IContractResolver 解析器)[0x000a2] 在 :0 在 SQLite.Net.SQLiteConnection..ctor (SQLite.Net.Interop.ISQLitePlatform sqlitePlatform,System.String 数据库路径,System.Boolean storeDateTimeAsTicks,SQLite.Net.IBlobSerializer 序列化器, System.Collections.Generic.IDictionary2[TKey,TValue] tableMappings, System.Collections.Generic.IDictionary2[TKey,TValue] extraTypeMappings,SQLite.Net.IContractResolver 解析器)[0x00000] 在 :0

【问题讨论】:

  • 您是否在平台和共享代码项目上都安装了 NuGet?
  • @Gerald Versluis- 我跨项目添加的包(我在添加包时选择了 3 个项目,包括共享项目)。但似乎没有在共享项目中安装包的选项。查看我更新的问题,我在其中添加了共享项目的屏幕截图。
  • 你试过用 sqlite-net-pcl 包代替吗?
  • @BrunoCaceiro 我以前没试过。但是现在当我安装它时,在更改了几个命名空间后,SQLite.Net.Platform 出现错误,平台不可用。
  • 使用 sqlite-net-pcl 包:github.com/praeclarum/sqlite-net 你使用的 Lib 已经 2 年没有更新了。

标签: c# sqlite xamarin xamarin.forms


【解决方案1】:

您使用的SQLite.Net-PCL nuget 不再由其作者维护。需要切换到其他类似的包sqlite-net-pcl

首先从所有项目中删除现有的SQLite.Net-PCL nuget,然后安装我上面提到的那个。从所有文件中删除所有旧的SQLite 相关引用。

您需要在SQLite 连接相关的依赖服务类中进行一些更改。然后你可以调整一些与SQLite相关的其他代码。通过为新的 nuget 包添加适当的 using 语句来解析引用。

我遇到了同样的错误,我通过切换到上面的 nuget 包解决了它。

注意:新的 nuget 包还没有 InsertOrReplaceAll 方法。所以如果你在之前的nuget中使用过这个方法,那么对于新的nuget,你需要创建一个InsertOrReplaceAll extension method

编辑:获取 SQLiteConnection:

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

在这个新的 nuget 中不需要通过平台。如果您不想将 DateTime 存储为 Ticks,请传递 false

【讨论】:

    【解决方案2】:

    2 个选择

    1. 更改 targetSdkVersion
    2. 将 libsqlite.so 添加到您自己的 lib 文件夹中。

    【讨论】:

    • @shingo- 你能告诉我在哪里可以获得 libsqlite.so 以及我的 lib 文件夹在哪里。
    • 忘记我的第二个选项,我认为 xamarin 或库的版本太旧了。看看xamarin.android_7.0
    • @shingo 那么请删除您的第二个选项
    【解决方案3】:

    有点晚了,但对于面临同样问题的人,您只需在本机代码中添加以下行

    [assembly: Xamarin.Forms.Dependency(typeof(AndroidSQLite))]
    

    如果这不起作用,请卸载现有的 sqlite-net-pcl 并安装 sqlite-net-pcl 这个库并重试

    【讨论】:

    • 不幸的是没有帮助这个。我也面临同样的问题。
    • 使用这个sqlite-net-pcl nuget 就可以了
    猜你喜欢
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多