Error when referring .Net Standard project in Xamarin Android Project

Viewed 1682

I'm getting this error when I add a reference to the .Net Standard project in the Andoid Project.

Severity Code Description Project File Line Suppression State Error Can not resolve reference: System.Threading.Tasks.Extensions, referenced by DotNetty.Transport. Please add a NuGet package or assembly reference for System.Threading.Tasks.Extensions, or remove the reference to DotNetty.Transport. VFMApp.Droid

I tried:

  1. Neither NuGet packages (System.Threading.Tasks.Extensions / DotNetty.Transport), clean solution, compiled project successfully, reference project, compile: ERROR

  2. NuGet package for DotNetty.Transport, clean solution, compiled project successfully, reference project, compile: ERROR

  3. NuGet package for System.Threading.Tasks.Extensions, clean solution, compile project successfully, reference project, compile: ERROR

  4. Both NuGet packages (System.Threading.Tasks.Extensions / DotNetty.Transport), clean solution, compile project successfully, reference project, compile: ERROR

Packages in .Net Standard project:

  • Acr.UserDialogs 7.0.1
  • Humanizer 2.5.16
  • Microsoft.Azure.Devices.Client 1.18.1
  • Microsoft.Azure.Mobile.Client 4.1.1
  • Microsoft.Azure.Mobile.Client.SQLiteStore 4.1.1
  • NETStandard.Library 2.0.3
  • Plugin.Share 7.1.1
  • Refractored.MvvmHelpers 1.4.1-beta
  • Xam.Plugin.Connectivity 4.0.0.190-beta
  • Xam.Plugin.DeviceInfo 4.1.0-beta
  • Xam.Plugin.Geolocator 4.5.4-beta
  • Xam.Plugin.Media 4.1.1-beta
  • Xam.Plugins.Settings 4.0.0.10-beta

Packages in Android project:

  • Xamarin.Android.Support.Compat 28.0.0
  • Xamarin.Android.Support.Design 28.0.0
  • Xamarin.GooglePlayServices.Maps 60.1142.1
1 Answers

I was able to get the same error as you when I referenced DotNetty.Transport from a Xamarin.Android project compiling/targeting against API Level 28. It seems to be an issue with System.Threading.Tasks.Extensions v4.5.1 that DotNetty.Transport is referencing. Here is a link to a similar problem from another project:

https://github.com/dotnet/reactive/issues/803

To sum up the issue, it looks like that Systems.Threading.Tasks.Extensions v4.5.1 has an empty profile for MonoAndroid10. Because the profile is empty, it can not resolve the System.Threading.Tasks.Extensions.dll file, thus the error.

enter image description here

To fix this, you have to update your android *.csproj file and redirect the reference to use the System.Threading.Tasks.Extensions.dll that is defined in the netstandard2.0 profile.

enter image description here

I performed the following steps:

  1. Add the System.Threading.Tasks.Extensions v4.5.1 nuget package to your Xamarin.Android project.
  2. Add the System.Runtime.CompilerServices.Unsafe v4.5.2 nuget package to your Xamarin.Android project
  3. Close Visual Studio.
  4. Open your Xamarin.Android *.csproj file with a Text Editor.
  5. Add another <ItemGroup> to tell your project to use the .netstandard2.0 profile instead of the MonoAndroid10 profile

<ItemGroup>
    <Reference Include="System.Threading.Tasks.Extensions">
        <HintPath>$(UserProfile)\.nuget\packages\system.threading.tasks.extensions\4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
    </Reference>
</ItemGroup>

So it should look something like this:

enter image description here

Save the file and reload your project in visual studio. After I did that I was able to compile successfully.

Related