I have made a library which depends on CEFsharp which requires to build the library for specific platforms. So no AnyCPU support.
Now I want to pack this into a NuGet. As far as I understand you have to put these files into the build folder and have a .targets
file which picks the correct dll to reference. So I ended up with a NuGet package looking like this:
lib
monodroid
MyLib.dll
xamarin.ios10
MyLib.dll
net45
MyLib.dll (x86)
build
net45
x86
MyLib.dll (x86)
x64
MyLib.dll (x64)
MyLib.targets
I put the following inside of the .targets
file:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="PlatformCheck" BeforeTargets="InjectReference"
Condition="(('$(Platform)' != 'x86') AND ('$(Platform)' != 'x64'))">
<Error Text="$(MSBuildThisFileName) does not work correctly on '$(Platform)' platform. You need to specify platform (x86 or x64)." />
</Target>
<Target Name="InjectReference" BeforeTargets="ResolveAssemblyReferences">
<ItemGroup Condition="'$(Platform)' == 'x86' or '$(Platform)' == 'x64'">
<Reference Include="MyLib">
<HintPath>$(MSBuildThisFileDirectory)$(Platform)\MyLib.dll</HintPath>
</Reference>
</ItemGroup>
</Target>
</Project>
So far so good. Now to the problem. When adding this NuGet to a new WPF project, I see the reference to the library appearing in the .csproj
file like:
<Reference Include="MyLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d64412599724c860, processorArchitecture=x86">
<HintPath>..\packages\MyLib.0.0.1\lib\net45\MyLib.dll</HintPath>
<Private>True</Private>
</Reference>
Although I don't see anything mentioned about the .targets
file. Is this still the way to do it with NuGet 3? Did I do something wrong? Currently this fails at runtime when running x64 because of the reference to the x86 lib.
There are a few things that I disagree with from the above solution. The nuspec file does not have to have any
<Files>
information if you use the default file structure by adding the files into a nuget folder. But if you want to run the nuget files from your solution folder then it is neededbetter nuspec content:
I also disagree that he has the .dll in the
build
folder, these should go in yourlib
folder and in thebuild
folder should contain your.targets
and.props
files. The documentation for nuget here mentions this.Also the
lib
folder are for .dll's that are in your references. Secondly, .dlls added to yourbuild
folder are supplementary dll's that might are needed in some cases-similar to how SQlite does it for it'sinterop
dll. So in this case, the better place to keep them is in the lib folder.Better folder structure:
.props file content:
.targets file content:
You can check that the above worked by opening your nuspec file with the Nuget Package Manager APP and checking all the files you added are picked up. Now when you add these to a project it will load the dll according to the architecure selected for the project it was referenced in. Also notice in the targets file it will raise an error if "Any CPU" is selected, and inform the user they must choose between x86 and x64
EDIT:
One point that was not clarified well was the naming of the .target and .props needs to be the same as the nuget package file name (meaning the version as well). As without this the double reference would appear when re-opening visual studio.
After fiddling with this for way too long time I figured it out.
Apparently the build.prop and build.target files need to have the exact same name as the NuGet package has, otherwise it will not be added in the csproj file.
EDIT:
I have created a small github repository showing how to use this for your own project.
It demonstrates a solution with 3 projects, one for iOS one for Android and a Net45 which targets both x86 and x64.
Notice, in the .props file. The paths are pointing at the folder structure of the unpacked NuGet. So these paths map to what you have put in your nuspec file.
So as in the repo, you define a nuspec like:
I put my x86 files in
build\x86
and x64 files intobuild\x64
. The name of the folder build could essentially be anything in this case, for these files. What matters here is that the props file, below points at these folders for the respective platforms.I am not sure whether putting the x86 lib into
lib\net45
even matters. You can experiment with that. The props file should pick the correct one for you anyways.In this case
$(MSBuildThisFileDirectory)
would be thebuild\net45\
folder, make sure your path correctly points at the dll's. Thebuild\net45
folder is important here. This is how NuGet automatically imports targets and props files.Also notice the name
My.Awesome.Library
is pretty consistent all around. What is important here is that the name of your props file, matches the NuGet package ID. Otherwise it seems like NuGet won't import it.To clarify the whole solution this is the way how it workes perfectly for me
.nuspec file
Folder Structure
.props file
.targets file
That's 100% working solution.