问题
I have the following dependencies in my .nuspec file:
<dependencies>
<group>
<dependency id="Dep1" version="2.4.11" />
<dependency id="Dep2" version="1.0.4" />
<dependency id="Dep3" version="1.0.4" />
<dependency id="Dep4" version="1.0.0" />
<dependency id="Dep5" version="1.0.4" />
<dependency id="Dep6" version="1.0.4" />
</group>
</dependencies>
I've created a local nuget server and I'm installing this NuGet package in a Xamarin Forms solution.
The thing is that in the Android part of the solution it will install, but in the .netstandard part it will complain that Dep 4, 5, 6 are only for MonoAndroid (which is correct). I don't need the dep 4,5 and 6 in my .netstandard solution.
NU1202: Dep4 1.0.0 is not compatible with netstandard2.0 (.NETStandard,Version=v2.0). Package Dep4 1.0.0 supports: monoandroid10 (MonoAndroid,Version=v1.0) NU1202: Package Dep5 1.0.4 is not compatible with netstandard2.0 (.NETStandard,Version=v2.0). Package Dep5 1.0.4 supports: monoandroid44 (MonoAndroid,Version=v4.4)
How can I separate dependencies per targetFramework? I've tried multiple variations of groups within the dependencies category, but nothing will solve this problem.
回答1:
After thoroughly reading
https://docs.microsoft.com/en-us/nuget/reference/nuspec#dependencies-element
https://docs.microsoft.com/en-us/nuget/reference/target-frameworks
https://docs.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/nuget-manual
it can be summed up to:
Version 2.0+
As an alternative to a single flat list, dependencies can be specified according to the framework profile of the target project using elements within .
Each group has an attribute named targetFramework and contains zero or more elements. Those dependencies are installed together when the target framework is compatible with the project's framework profile.
The element without a targetFramework attribute is used as the default or fallback list of dependencies. See Target frameworks for the exact framework identifiers.
Therefore in my case it would be:
<dependencies>
<group targetFramework="MonoAndroid10">
<dependency id="Dep1" version="2.4.11" />
<dependency id="Dep2" version="1.0.4" />
<dependency id="Dep3" version="1.0.4" />
<dependency id="Dep4" version="1.0.0" />
<dependency id="Dep5" version="1.0.4" />
<dependency id="Dep6" version="1.0.4" />
</group>
<group>
<dependency id="Dep1" version="2.4.11" />
<dependency id="Dep2" version="1.0.4" />
<dependency id="Dep3" version="1.0.4" />
</group>
</dependencies>
This way when I'm installing the package on the .netstandard project it will check the targetFramework and see that it's not MonoAndroid and it will fallback to the element without a targetFramework and use only Dep1, Dep2 and Dep3. For the MonoAndroid10 project it will do the same and use all of them.
来源:https://stackoverflow.com/questions/52074984/target-specific-dependencies-nuget