tipa
tipa

Reputation: 437

Using EF7 SQLite on Android 7+

In my project, I am using the Microsoft.EntityFrameworkCore.SQLite package from NuGet. But for Android 7+, the app crashes with unauthorized access to "/system/lib64/libsqlite.so".

This is because Android 7 doesn't allow native libraries other than the ones in their NDK (as far as I understood). Also see this issue: https://github.com/aspnet/EntityFramework/issues/7777.

So, I researched the internet for about 2 days now and unsuccessfully tried a bunch of things to get EFCore to work on Android 7. One of them was to update to the preview version v2.0.0-preview1-final. But this resulted in other problems like the following where the app compiled, but crashed while deploying/starting on the emulator:

06-21 09:58:18.970 D/Mono    ( 3632): Assembly Loader probing location: 'System.Runtime.CompilerServices.Unsafe'.
06-21 09:58:18.970 F/monodroid-assembly( 3632): Could not load assembly 'System.Runtime.CompilerServices.Unsafe' during startup registration.
06-21 09:58:18.970 F/monodroid-assembly( 3632): This might be due to an invalid debug installation.

I then tried to add the 'System.Runtime.CompilerServices.Unsafe' NuGet package manually to my project, but it didnt fix the problem. I messed around a lot, trying different build parameters and such and actually got the app running with the preview package of EFCore on an Android 7 emulator. However, after a project clean and rebuild, the error showed up again.

Any help on how to get EFCore Sqlite to work on Android 7?

I am using Visual Studio 2017 with Xamarin.Android 7.3.1.2

I created an example project in order to reproduce the issue: https://github.com/tipa/EFCoreSample - Basically just started with the basic Android template from VS and added the Nuget package

Upvotes: 4

Views: 637

Answers (2)

Hugo Dantas
Hugo Dantas

Reputation: 81

I had the same issue and I found a solution to this problem.

I'm using Xamarin.Forms applications with .Net Standard 2.0 libs and using EFCore with SQLite. All you have to do is edit your Android csproj and add this after the last PropertyGroup tag:

<PropertyGroup>
  <NoWarn>$(NoWarn);NU1605</NoWarn>
</PropertyGroup>

And then add the reference for System.Runtime.CompilerServices.Unsafe in csproj:

<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.3.0" />

Clean and Rebuild your Android Project.

I found the answer to the problem here: https://developercommunity.visualstudio.com/content/problem/152947/xamarin-android-run-error-could-not-load-assembly.html

Upvotes: 0

Vacarescu Catalin
Vacarescu Catalin

Reputation: 11

I know this was asked more than two months ago and you've probably found the solution, but I've wanted to post how we've managed to get around this issue.

We are developing a Xamarin.Forms application with EntityFrameworkCore and SQLite. Due to project and company policies, we are restricted to using .NetStandard 1.6 (we migrated our PCL projects from .NetFramework to .NetStandard in order to integrate EntityFrameworkCore).

After this issue started happening to us, we managed to fix this by doing the following:

  1. Updating "Microsoft.EntityFrameworkCore" and "Microsoft.EntityFrameworkCore.SQLite" from version "1.1.1" to "2.0.0-preview1-final" (this is the latest version that works with .NetStandard 1.6) in the DAL project (we have a separate PCL project that contains the Context, UoW, Repositories and Models)

  2. Updating just "Microsoft.Data.Sqlite" (this automatically updates the needed dependencies) from version "1.1.1" to "2.0.0-preview1-final" in the main Xamarin.Forms Android project

EntityFrameworkCore versions 2.x.x use the Android 7+ "approved" way of accessing the "libsqlite.so" file, while versions 1.x.x do not.

Hope this is of help to anyone who might use similar packages that we had.

Upvotes: 1

Related Questions