Bug 59313 - Compilation error in Android project when using .Net Standard NuGet package in .Net Standard base library
Summary: Compilation error in Android project when using .Net Standard NuGet package ...
Status: CONFIRMED
Alias: None
Product: Android
Classification: Xamarin
Component: MSBuild ()
Version: 8.0 (15.4)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Ankit Jain
URL:
: 60046 ()
Depends on:
Blocks:
 
Reported: 2017-09-07 21:30 UTC by Chris Bergmeister
Modified: 2018-02-15 16:07 UTC (History)
10 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test case (94.14 KB, application/x-rar)
2017-09-08 09:46 UTC, Denys Han
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 59313 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Chris Bergmeister 2017-09-07 21:30:22 UTC
I have followed the guides to replace the Xamarin PCL library with a .Net Standard 2.0 library but if this library uses a method like e.g. Newtonsoft.Json.JsonConvert.DeserializeObject("") from the NuGet package Newtonsoft.Json, the Android project throws the following compilation error:

Severity Code Description Project File Line Suppression State Error Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. Perhaps it doesn't exist in the Mono for Android profile? File name: 'Newtonsoft.Json.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.AddAssemblyReferences(DirectoryAssemblyResolver resolver, ICollection`1 assemblies, AssemblyDefinition assembly, Boolean topLevel) at Xamarin.Android.Tasks.ResolveAssemblies.Execute(DirectoryAssemblyResolver resolver) MyAppName.UI.Xamarin.Android

I am using the latest version of Visual Studio 2017, the latest version of Xamarin and Xamarin.Forms and the latest Windows 10 updates. I am compiling against Android 7.0. Note that this only happens for .Net Standard libraries and I am not sure if this is a .Net Standard bug, they have a new issue that seems to be related to this, see here: https://github.com/dotnet/announcements/issues/31
Comment 1 Chris Bergmeister 2017-09-07 21:34:10 UTC
I should add that the bug even occurs if I downgrade the library to .net standard 1.3 so it seems to be not 2.0 but .net standard specific
Comment 2 Cody Beyer (MSFT) 2017-09-08 00:14:18 UTC
Hey,

Are you able to attach a minimized test case which demonstrates this issue? I want to ensure that any attempts to reproduce and solve the issue are true to your use case.

Thanks!
Comment 3 Denys Han 2017-09-08 09:46:03 UTC
Created attachment 24640 [details]
Test case

I've got the same issue, here's the simple test project attached.
As soon as call to JsonConvert.DeserializeObject is removed, everything compiles properly.
Comment 4 Denys Han 2017-09-08 12:32:15 UTC
Forgot to mention, this issue happens with all .net standard libraries, not just Newtonsoft.Json. Xamarin.iOS seems to handle everything fine in same situation.
Comment 5 Chris Bergmeister 2017-09-08 18:33:33 UTC
Thanks for providing the minimal sample. I'm glad I'm not the only one with this problem. I found that as a temporary workaround, one can install the NuGet package in the Android project itself to get rid of this error.
Yes, I agree, this problem does not seem to be neither specific to Newtonsoft.Json nor to the version of .Net Standard being used in the class library.
Comment 6 Jon Douglas [MSFT] 2017-09-08 22:21:13 UTC
I feel like the issue here is the fact that you do not have Newtonsoft.Json included in your Xamarin.Android project. The big distinguishing factor here is that Xamarin.iOS does not have the same issue. It doesn't need a reference in the platform project to resolve.

If I were to guess, it would be an issue with how NuGet resolves the library in the netstandard project when referenced from Xamarin.Android, or it is an issue within Xamarin.Android picking it up. Either way, we can all agree that this is something that can be looked into further. Thus I am setting this bug to CONFIRMED.

FYI:

Xamarin.Android 7.5/8.0+ supports netstandard 2.0 items as per the following page:

https://docs.microsoft.com/en-us/dotnet/standard/net-standard
Comment 7 Chris Bergmeister 2017-09-10 19:10:16 UTC
@Jon Douglas: Thanks for confirming the bug.
If I use a NuGet package API in a class library then I expect to only need to add the NuGet package reference to this library and not the Android project as well. This is the behaviour of PCL libraries when being used as Xamarin BCLs, therefore I expect the same for NetStandard libraries. The reason why the workaround of just adding the NuGet package to the Android project as well is a real issue is because having to add all used NuGet packages to the Android projects creates a NuGet dependency/versioning hell (due to the weird dependencies/incompatibilities of all the Android Xamarin NuGet packages it is a tedious trial and error process to find a combination of NuGet packages that work nicely with each other).
P.S. Although the bug applies to any version of the netstandard, you might want to have a look at this issue about netstandard2.0 and NuGet: https://github.com/dotnet/announcements/issues/31
Comment 8 dean.ellis 2017-09-12 09:56:57 UTC
@Jon Douglss, while the iOS app compiles it does not run as it does not include the Newtonsoft.Json.dll in the final app package. 
I think both iOS and Android have the same problem, Android's build process exposes it earlier. 

We should ask the msbuild team how to go about solving this issue.
Comment 9 Chris Bergmeister 2017-09-23 17:50:59 UTC
Any updates?
Comment 10 dean.ellis 2017-09-25 13:04:31 UTC
This problem seems to affect both Android and iOS so it is now with our MSBuild Team to figure out why the dependencies for libraries are not being picked up.
Comment 11 Chris Bergmeister 2017-09-25 18:08:43 UTC
Since MSBuild is now open source, could you please open an issue on their GitHub repository below so that everyone can follow the discussion please:
https://github.com/Microsoft/msbuild/issues
Comment 12 Chris Bergmeister 2017-10-09 23:34:33 UTC
Since there does not seem to be much progress here, I opened a bug directly at MSBuild:
https://github.com/Microsoft/msbuild/issues/2615
Comment 13 Jairo Marques 2017-11-14 00:18:07 UTC
Any news?
Comment 14 dean.ellis 2017-12-01 11:41:59 UTC
*** Bug 60046 has been marked as a duplicate of this bug. ***
Comment 15 Ghaladen 2017-12-03 00:09:07 UTC
I got around this error by adding Newtonsoft.Json 10.0.3 to the Android project. Hope that helps
Comment 16 duckodil 2017-12-29 22:16:26 UTC
At least the workaround is there, even if it does get messy... but do we have any update on this one?
Comment 17 jzeferino 2018-02-15 16:07:54 UTC
Any updates on this?

The workaround of adding Newtonsoft.Json works but I don't want to have that dependency explicitly there since it pull +20 packages due to .netstandard.