Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a DNX project, how can I target for Xamarin Android?

TL;DR

I'm writing a Xamarin.Android app and want to reference the NuGet package of a library of mine that is a dotnet-targeted DNX class library. When I do this, the compiler spits out the warning

The type 'DateTime' is defined in an assembly that is not referenced. 
You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, ...

and the IDE complains with messages like

Argument type 'System.DateTime [mscorlib, Version=2.0.5.0, Culture ...]' is
not assignable to parameter type 'System.DateTime [mscorlib, Version=4.0.0.0, ...]'

although the builds succeed and the code works. How can I change that in the library / the project.json?

Long story

At work we're currently porting our projects to project.json-type ones (Package, DNX, whatever the name is). At the moment, we're running the beta 7 Visual Studio integration and, in general, everything works out just fine. I now want to reuse one of our model libraries within an ASP.NET 5 and a Xamarin.Android project (from a NuGet feed).

Since we had absolutely no luck with class libraries targeting .NET 4.5.1 in the Xamarin project I migrated the model library to a DNX project targeting net45, dnx45 and dotnet (in favor of dnxcore50, as described here), the frameworks part in the project.json being

"frameworks": {
  "net45": {
    "frameworkAssemblies": {
      "mscorlib": "4.0.0.0",
      "System.Xml": "4.0.0.0",
      "System.Collections.Concurrent": "4.0.0.0"
    }
  },
  "dnx45": {
    "frameworkAssemblies": {
      "mscorlib": "4.0.0.0",
      "System.Xml": "4.0.0.0",
      "System.Collections.Concurrent": "4.0.0.0"
    }
  },
  "dotnet": {
    "dependencies": {
      "System.Collections": "4.0.0",
      "System.Linq": "4.0.0",
      "System.Runtime": "4.0.0",
      "System.Reflection": "4.0.0",
      "System.Runtime.Extensions": "4.0.0",
      "System.Threading": "4.0.0",
      "System.Text.RegularExpressions": "4.0.0",
      "System.Text.Encoding": "4.0.0",
      "System.Collections.Concurrent": "4.0.0"
    }
  }
},

Although this article suggests using net45 as a target for monoandroid51 projects, the dotnet library is instead referenced by the Android project whenever I add the NuGet package to it.

The package.json then contains

<package id="My.Awesome.Lib" version="1.2.3-foo9" targetFramework="monoandroid51" />

and the .csproj there has

<Reference Include="My.Awesome.Lib, Version=1.2.3.0, Culture=neutral, processorArchitecture=MSIL">
  <HintPath>..\packages\My.Awesome.Lib.1.2.3-foo9\lib\dotnet\My.Awesome.Lib.dll</HintPath>
  <Private>True</Private>
</Reference>

This works out so far unless I have version numbers higher than 4.0.0 in the dependencies part and basically combusts when I do, however the following

However when I build the project, I'll get the compiler warning

1>C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1706,3): 
    warning MSB3277: Found conflicts between different versions of the 
    same dependent assembly that could not be resolved.  These reference
    conflicts are listed in the build log when log verbosity is set to detailed.

right after the library reference. Within Visual Studio, whenever I pass a value from the Android project to one of the library types, I'll get the red squiggly lines with a message stating

The type 'DateTime' is defined in an assembly that is not referenced. 
You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, ...

as well as

Argument type 'System.DateTime [mscorlib, Version=2.0.5.0, Culture ...]' is
not assignable to parameter type 'System.DateTime [mscorlib, Version=4.0.0.0, ...]'

which makes perfect sense, since the Xamarin BCL is labeled as runtime version v2.0.50727, whereas my library is v4.0.30319.

I'm now wondering if I need to target some PCL something or if there is something else I'm missing.

like image 954
sunside Avatar asked Nov 10 '22 05:11

sunside


1 Answers

This is not YET supported. To support this scenario, nuget will need to also be updated to take in charge the new monikers that are being thought about to simplify the references.

In the future, you would just target netstandard1.4 instead of dnxcore50 and you would have everything running and compatible with Xamarin.

.NET Standard Platform

This is where things will be going. To simplify the madness of targeting multiple platforms. This is where I got my netstandard* moniker and of course, until it hits release... this is still subject to change.

like image 88
Maxime Rouiller Avatar answered Nov 14 '22 23:11

Maxime Rouiller