While working on upgrading an existing project from Xamarin.Forms 2.3 to Xamarin.Forms 3.0 the following, incredibly baffling, error was thrown during the build phase:
Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly ‘Xamarin.Android.Support.Fragment, Version=18.104.22.168, Culture=neutral, PublicKeyToken=’. Perhaps it doesn’t exist in the Mono for Android profile?
File name: ‘Xamarin.Android.Support.Fragment.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) EdgeApp.Droid
The fix that worked for me – Uninstalling Xamarin.Forms then reinstalling:
- Document all Nuget packages that depend on Xamarin.Forms – their versions and what project(s) they’re installed in.
- Also document the version and which projects Xamarin.Forms is installed in.
- Uninstall the packages from step 1 from all projects
- Uninstall Xamarin.Forms from all projects
- Reinstall Xamarin.Forms
- Reinstall packages that depended on Xamarin.Forms
- Rebuild solution
Other suggested fixes:
Refresh Nuget package cache
First, close your solution.
In your solution, normally at the top level, there will be a “packages” folder. You can safely delete the contents of this folder unless you have manually placed packages here. If you’re worried then take a backup first.
Now you can reopen your solution. Visual Studio 2017 should automatically restore all packages. Once it has done this then you can attempt to rebuild your solution.
Reinstall Nuget packages
In Visual Studio 2017:
Tools -> Nuget Package Manager -> Package Manager Console
For each project run the following command:
Once the command(s) have completed running you can attempt to rebuild your solution.
Update Target Android version
Our project was targeting an older version of Android originally (6.0) and updating our project to target the latest stable version (8.1) didn’t resolve the error. However it may help you.
In Visual Studio 2017, right click on your Android project and select “Properties”. On the left hand side of the window that opened, click on “Android Manifest”. In “Target Android Version” select a newer version that what you’re currently targeting.
You can also unload your solution and edit the Android project .csproj file and look for the <TargetFrameworkVersion> element. You can update this string to a valid Android version e.g. <TargetFrameworkVersion>v8.1</TargetFrameworkVersion> for Oreo.
Reload your solution, clean the solution, then rebuild.