Could not load assembly 'System.Runtime.CompilerServices.Unsafe' during startup registration


#1

Started with a new project in VS2017 from Prism template. Added PropertyChanged.Fody nuget package (and the xml file) - ran the basic project and all fine.
Add the Realm nuget (adding the <RealmWeaver /> line to the FodyWeavers file).
Now when I deploy and run the app on Android emulator the app crashes on startup with the following output:

Could not load assembly 'System.Runtime.CompilerServices.Unsafe' during startup registration.
This might be due to an invalid debug installation.
A common cause is to 'adb install' the app directly instead of doing from the IDE.

Now I am pretty sure this is a VS/Xamarin issue, not a Realm issue, but the only clues I have been able to find on the net are in some github issues which are all a bit above me, and I wondered if anyone here had run into this issue and could give me a fix or workaround in plain English?

For example, https://developercommunity.visualstudio.com/content/problem/152947/xamarin-android-run-error-could-not-load-assembly.html - I tried changing the Linker options and my Nuget is set to use packages.config and no luck but its all a bit in-depth.


#2

Alternative workaround was proposed here: https://github.com/xamarin/AndroidSupportComponents/issues/75#issuecomment-361282393

Basically, you have to switch to the old packages.config as opposed to the <PackageReference> tag in your csproj. Here are the steps proposed (I have not tried it, so can’t vouch for it):

  1. Remove all your Nuget packages from the Android project.
  1. In Options>NuGet Package Manager>General make sure “Allow format selection on first package install” is checked.
  2. Add a package to your Android project, choosing Packages.config
  3. Add the rest of your packages

#3

Thanks @nirinchev but I could not get this to work. My package manager was already set to Packages.config, but I went through the steps and removed them all, chose Packages.config again and re-installed them. Still get exactly the same error when I run the app.


#4

Overwrite the System.Runtime.CompilerServices.Unsafe.dll to correct version (folder …/obj/debug i think). It`s a bug from Visual Studio. The correct version you will find on Nuget package. Cleaning the project will not work, because Visual Studio creates wrong dll version everytime that dll is removed.


#5

It can be resolved.

  1. Go to C:\Users%user%.nuget\system.runtime.compilerservices.unsafe\4.4.0, and delete “ref” folder then make a copy of “lib” folder and rename the copy back to “ref”.
  2. Cleanup all the “bin” and “obj” folders in the projects.
  3. Rebuild and run…

#6

Al last - it works. I have seen several different ‘solutions’ to this which mostly involved manually editing the project file to remove package references, putting all the packages back in to the csproj, and just kept running into all sorts of other problems like errors about multiple targets and stuff - a true nightmare that, try as I might, I could never fully resolve. Now this simple solution has fixed it (at least for now). Thanks @hongliyu2002


#7

@hongliyu2002 Gracias!!!


#8

It works! Really appreciate for your help!


#9

Many a thanks, spent a good 2 hours trying to fix xamarin build scripts, my first search should’ve included realm in it… thanks again