0

The setup is like this:

  • A Xamarin.Android application, which depends on Android class library (at least that's what the template is called in VS)
  • Said class library, the purpose of which (not entirely relevant, but FYI) is interfacing with a REST service and has a dependance on the famous Newtonsoft.Json NuGet package.
  • An NUnit test project for said library.

IDE is Visual Studio 2017, latest version.

If you build and deploy the app on the phone, everything is fine. However, if you try to run tests from the tests project, it says that it can't find the Newtonsoft library.

I've even managed to find a sort of reason: when the library gets built, it's dependancies aren't packed inside, and they are not copied to build directory. When .apk is built for the phone, the dependancies ARE getting packed inside. However, when NUnit project builds itself, it only takes the library, and the dependancies are nowhere to be found. However, there's no interface to control the behaviour of NuGet "Package Reference" type dependencies (blue ones), the properties window is empty for them. And I found no way to add NuGet packages to this kind of project as a ".config" type of dependancy (grey one).

There is a workaround - you can add the Newtonsoft package to the NUnit test project, then it gets copied to the build directory and the Android library works with it, however it doesn't feel right to me. Tests don't need that reference and it has no business in that project.

Misamoto
  • 332
  • 1
  • 11
  • I would ask why you are using an Android class library for interfacing with a REST service. Android class libraries are more Android-specific in terms of using the Android Framework APIs to provide platform specific libraries. Interfacing with a REST service, this would make more sense to use a .NET Standard library for your "domain/core" services which you would use an NUnit/XUnit .NET Core test project to test against. – Jon Douglas Jul 20 '18 at 18:01
  • I'm not too experienced. While there's a lot I know that lets me sound knowledgable, there's a lot I don't so I can make noobie mistakes. I'm writing an Android app, so I chose an Android library, that's the entire logic behind it. – Misamoto Jul 21 '18 at 11:41

2 Answers2

0

How to control NuGet packages in Android Class Library?

Your workaround is the correct solution, you don't need to worry too much about it.

That is because the Newtonsoft package is not used directly in your NUnit test project, so Visual Studio / MSBuild doesn't know if your test project needs this Newtonsoft library. In order to avoid reference pollution in NUnit test project, Visual Studio / MSBuild tries to only bring references over into NUnit test project that it detects as being required by project Xamarin.Android application.

So, to resolve this issue, we often add Newtonsoft to the test project or give a copy task to copy it to the test project.

See This Thread for some more details.

Leo Liu-MSFT
  • 52,692
  • 7
  • 69
  • 81
0

Looking for an answer to another question I now found info that my described behaviour is a known problem, described by .NET developers here: https://github.com/dotnet/standard/issues/481

Misamoto
  • 332
  • 1
  • 11