0

I'm a longtime ASP.NET developer but new to Xamarin. I have a pretty simple NETStandard Xamarin project for Android built on Core 2.0 inside Visual Studio 2017. I've just begun building my app. In the common project I am using Microsoft.Data.Sqlite to work with a local database. That's the only library I'm pulling in besides Xamarin.Forms and NETStandard.

EDIT: I should note that this application used to compile just fine until I upgraded from Android API 26 to 27. I haven't changed any code. The problem isn't my code but either the project configuration or NuGet.

Here is the compile-time error that is not associated with any particular file:

Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'Microsoft.Data.Sqlite, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Perhaps it doesn't exist in the Mono for Android profile? File name: 'Microsoft.Data.Sqlite.dll' at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve(AssemblyNameReference reference, ReaderParameters parameters) at Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(DirectoryAssemblyResolver resolver, ICollection`1 assemblies, AssemblyDefinition assembly, Boolean topLevel) at Xamarin.Android.Tasks.ResolveAssemblies.Execute(DirectoryAssemblyResolver resolver)

I do not know why the Android app needs Sqlite, as the database stuff is being handled by the common project. This seems like a red herring but I can't make this error go away.

Here is NuGet for MyApp (common project):

NuGet screenshot for common project

Here is NuGet for MyApp.Android:

NuGet screenshot for MyApp.Android

My first instinct is this long list isn't right, but all I did was Install-Package Xamarin.Forms and all these other things came with it. Prior to installing Xamarin.Forms, I removed all NuGet packages. I've been fighting NuGet for a while so the "default" list isn't the same anymore. I've had Crosslight in here at one point, among other things, possibly Sqlite. When I create a brand-new Xamarin Forms app, there are only six packages: Xamrin.Forms (v3.1.0), Xamarin.Android.Support.v4 (v27.0.2), ...v7.AppCompat (v27.0.2), ...v7.CardView (v27.0.2), and ...v7.MediaRouter (v27.0.2), though they are not the latest versions.

I've exhausted Google. The closest I could find to resolving this is this GitHub issue for Mono, but the specific library is System.Drawing.Common, not Microsoft.Data.Sqlite. As that thread stands, it's a bug with Mono. If so, what is the workaround?

Should I be concerned that MyApp.Android is mixing Support.v4 and Support.v7?

If it's a clue, MyApp has gone through a lot of massaging and playing around with NuGet to get various things to work. I've installed many libraries and removed them later. I've edited the .sln and .csproj files, I've deleted the Packages, obj, and bin folders multiple times. (Sqlite is not referenced in the .csproj file.)

I've tried copying my code from MyApp to a fresh Xamarin Forms MyApp2 but that has different issues (not finding the Sqlite database and crashing when displaying a toast notification, despite having identical code). MyApp didn't have either of these issues last week when everything was running and before I tried upgrading the API from 26 to 27.

And yes, I've Cleaned and Rebuilt my project innumerable times, as well as cleared the NuGet cache.

I am trying to use the (latest) Android API 27. I believe I created MyApp under API 26 but I'm not certain.

All I want to do is create a Xamarin Forms app for Android API 27 that uses Microsoft.Data.Sqlite! Why is this so hard? I've spent over 90% of my time on this project fighting code I didn't write!

Eric
  • 654
  • 5
  • 16

1 Answers1

0

Xamarin Forms/Visual Studio can easily get confused. Sometimes certain errors may go away just be restarting VS. I generally will close VS, nuke all obj + bin directores, and nuke the .vs directory. That will clean a lot of the cached cruff. Always close + reopen VS after change the major XF versions. (2.0<->2.5, 2.5<->3.0, 3.0<->3.1, etc.)

Beyond that, due to the large dependency tree of nuget packages, both explicitly referenced, and implicitly, it's possible to get into situation where various nuget packages won't be compatible.

Mono.Data.Sqlite is a strange case because there is some redundancy between the mono assemblies and the .net assemblies, and you have to specifically add that assembly in. See: https://forums.xamarin.com/discussion/99947/how-to-reference-mono-data-sqlite You may have to explicitly go and find that assembly and add it to your project. bleh.

Generally I don't use that for sqlite, and prefer https://github.com/praeclarum/sqlite-net and https://github.com/bordoley/SQLitePCL.pretty. I realize that other nuget packages may depend on Mono.Data.Sqlite

Curtis Shipley
  • 7,990
  • 1
  • 19
  • 28
  • Deleting the .vs folder (in addition to bin and obj and packages) was something I hadn't done before. Opened VS. Clean, rebuild. Got another error. Restarted VS again and the original error returned. Adding Mono.Data.Sqlite.Portable didn't help. Adding Microsoft.Data.Sqlite.Core got my Android app to compile again. I have no idea why I need it in the Android project, though. – Eric Jul 27 '18 at 21:21
  • Weeks ago I had problems getting Sqlite-net and SQLitePCL to work, but I had success with Microsoft.Data.Sqlite. Now, though, I'm getting a runtime error, `You need to call SQLitePCL.raw.SetProvider()`, so I might be returning to PCL. Very strange, Microsoft.Data.Sqlite worked great in API 26. But this is getting off-topic; you answered my initial question. – Eric Jul 27 '18 at 21:24
  • It's probably because another nuget package requires it. I'm not sure if you tried this, but remove the `Microsoft.Data.Sqlite`, and try adding the assembly for `Mono.Data.Sqlite`. – Curtis Shipley Jul 27 '18 at 22:20
  • I wrote a couple blog posts about this kind of thing that may be of interest. They will not fix this particularity issue though. https://www.saltydogtechnology.com/xamarin-sos-problems/ and https://www.saltydogtechnology.com/xamarin-nuget-gotchas/ – Curtis Shipley Jul 27 '18 at 22:21
  • Thanks for the explanation and link. It's opening my eyes. I entered my Xamarin project treating NuGet pretty callously. I'm learning I need to tread very carefully! Also, I got Microsoft.Data.Sqlite to work again by adding `install-package sqlitepclraw.bundle_green`. I don't know why `install-package microsoft.data.sqlite` didn't add this automatically. Now to solve why my Android `Toast.MakeToast()` is crashing the app.... – Eric Jul 30 '18 at 15:02