Xamarin.Essentials has to be my favorite project that I have worked on at Xamarin so far. Bringing together over 30 native platform specific APIs into a single API that creates a linker safe and optimized library that so far has really delighted developers. I did some surveys last week and everyone could not stop saying positive things about the library, which is awesome! One thing stood out though that seems to be causing issues, which is how NuGet handles installing and updating Android Support Libraries.
I recently wrote an article outlining exactly what each of the Android API Levels means. It also highlights why you may see Version conflict detected when installing a library that depends on a support library and how to solve it. My friend Jon Dick took it a step farther and went into low level details on his blog and created an awesome video and we even added troubleshooting to our docs on how to solve this. I figure why not try again one more time on how to solve this if you are having issues installing Xamarin.Essentials, plugins, or other Xamarin.Android libraries.
Compile Against the Latest
Notice that I said COMPILE not Target! When we want to use newer Android Support Libraries we must ensure that we are compiling our android app against the correct version of Android. You can see what version of Android you must compile against to install a NuGet package for Android based on the dependency. Note that Xamarin.Essentials lists MonoAndroid,Version=8.1 aka Android 8.1 SDK 27.
As of today when you install Visual Studio 2017 you will receive Android 8.1 SDK, but if you have an older project you may have it compiling against an older version. Simply right click on your project and under Application you will see Compile using Android version: (Target Framework) and set it to Android 8.1.
Remember that this is the version of Android we use to COMPILE your application and has no effect over Minimum or Target Android versions, which live in the Android Manifest section of the project properties.
Resolving Version Conflict
I will not go too deep into why or how this is happening because Jon already did this in his post. Just remember that when you attempt to install Xamarin.Essentials or other packages that use Support Libraries you want to ensure that all of your Support Libraries are up to date and use the same version. In the case of my application I had Support version 27.0.2 installed, but Xamarin.Essentials required 188.8.131.52 of CustomTabs and Core.Utils which were not yet in my project so I received:
Version conflict detected for Xamarin.Android.Support.Compat. Reference the package directly from the project to resolve this issue. App10.Android -> Xamarin.Essentials 0.8.0-preview -> Xamarin.Android.Support.CustomTabs 184.108.40.206 -> Xamarin.Android.Support.Compat (= 220.127.116.11) App10.Android -> Xamarin.Android.Support.Design 27.0.2 -> Xamarin.Android.Support.Compat (= 27.0.2).
This error makes sense because I have 27.0.2 of a Support Library that depends on Support.Compat, but CustomTabs also depends on this library and needs 18.104.22.168 and NuGet doesn't know what to do.
There are a few solutions here:
Option 1: Update All NuGets First
If we know that Xamarin.Essentials needs 22.214.171.124 then just update all of your Support Libraries to this version.
After doing this you will be able to install Xamarin.Essentials.
Option 2: Install Dependencies
One thing to do is not only update your Support Libraries, but also install any of the Support Library dependencies that the NuGet needs. You simply want to match the current version of the other support libraries first or update to 126.96.36.199 and then install both CustomTabs and Core.Utils.
Option 3: Update PackageReferences (My Favorite)
When all else fails or if you are like me and are tired of messing with the NuGet UI then you can just open up your Android projects .csproj and update the PackageReferences to ensure all support libraries are on 188.8.131.52 and also add the other libraries you need. Here is what mine looks like:
<PackageReference Include="Xamarin.Essentials" Version="0.8.0-preview" /> <PackageReference Include="Xamarin.Android.Support.Design" Version="184.108.40.206" /> <PackageReference Include="Xamarin.Android.Support.v7.AppCompat" Version="220.127.116.11" /> <PackageReference Include="Xamarin.Android.Support.v4" Version="18.104.22.168" /> <PackageReference Include="Xamarin.Android.Support.v7.CardView" Version="22.214.171.124" /> <PackageReference Include="Xamarin.Android.Support.v7.MediaRouter" Version="126.96.36.199" /> <PackageReference Include="Xamarin.Android.Support.Core.Utils" Version="188.8.131.52" /> <PackageReference Include="Xamarin.Android.Support.CustomTabs" Version="184.108.40.206" />
When you re-load your project the NuGet packages will re-load and everything will be happy.
That is it! We have resolved all of our issues and you can continue coding! Leave comments below if you have any other questions.