0
votes

I am using Sqlite in a Xamarin Forms Portable project and I successfully run my application on iOS and UWP. On Android, I can run my application on a Android 6.0 emulator but in Android Oreo although app is installed, it crashes on startup. I am facing the following issue.

Issue:

android.runtime.JavaProxyThrowable: 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.SQLiteConnectionWithLock..ctor (SQLite.Net.Interop.ISQLitePlatform sqlitePlatform, SQLite.Net.SQLiteConnectionString connectionString, System.Collections.Generic.IDictionary2[TKey,TValue] tableMappings, System.Collections.Generic.IDictionary2[TKey,TValue] extraTypeMappings) [0x0002e] in <8f2bb39aeff94a30a8628064be9c7efe>:0 at project.Models.Database+<>c__DisplayClass1_0.<.ctor>b__0 () [0x0001d] in <7df232f34ab8474d9153e3809af4eda8>:0 at SQLite.Net.Async.SQLiteAsyncConnection.GetConnection () [0x00000] in <563d605f9f014eeeb32fd4a27b4d142e>:0 at SQLite.Net.Async.SQLiteAsyncConnection+<>c__DisplayClass11_0.b__0 () [0x00006] in <563d605f9f014eeeb32fd4a27b4d142e>:0 at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in :0 at System.Threading.Tasks.Task.Execute () [0x00010] in :0 at mono.java.lang.RunnableImplementor.n_run (Native Method) at mono.java.lang.RunnableImplementor.run (RunnableImplementor.java:30)
at android.os.Handler.handleCallback (Handler.java:789) at android.os.Handler.dispatchMessage (Handler.java:98) at android.os.Looper.loop (Looper.java:164) at android.app.ActivityThread.main (ActivityThread.java:6541) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)

In order to create the connection:

 public Database(string databaseName)
        {
            var pathToDatabaseFile = DependencyService.Get<Interfaces.ISQLite>().GetLocalPathToFile(databaseName);
            var platform = DependencyService.Get<Interfaces.IPlatformProvider>().GetPlatform();

            _connection = new SQLiteAsyncConnection(() =>
            new SQLiteConnectionWithLock(platform, new SQLiteConnectionString(pathToDatabaseFile, false)));
            _connection.CreateTableAsync<SqlitePage>().Wait();
        }

In every project GetPlatform and GetLocalPathToFile are implemented:

    return new SQLitePlatformAndroid();

and

return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), fileName);

The packages I use for Sqlite are:

  • SQLite.Net.Async-PCL
  • SQLite.Net.Core-PCL
  • SQLite.Net-PCL

Any ideas?

1

1 Answers

0
votes

You should change to sqlite-net-pcl, since Android 6+, some permission have changed.

https://www.nuget.org/packages/sqlite-net-pcl/

I switched to this PCL, and everything works like a charm.