Manually Creating NuGet Packages for Xamarin
This page contains some tips to help build NuGet packages that target the Xamarin platform.
Xamarin Studio 6.2 (and Visual Studio for Mac) includes the ability to automatically generate NuGet packages from PCL, .NET Standard, or Shared Projects. Refer to the Multiplatform Libraries for Code Sharing guide for more details.
NuGet Package Xamarin Profiles
The NuGet website's Supporting Multiple .NET Framework Versions and Profiles discusses how to support different Microsoft frameworks and profiles but does not include the target framework names used by Xamarin.
The main Xamarin target frameworks in use today are:
- MonoAndroid - Xamarin.Android
- Xamarin.iOS - Xamarin.iOS Unified API (supports 64-bit)
- Xamarin.Mac - Xamarin.Mac's mobile profile, which is equivalent to the Xamarin.iOS and Xamarin.Android API surface.
There is also a target for the older iOS Classic API:
- MonoTouch - iOS Classic API
A .nuspec file that targeted all these would look something like:
<files> <file src="Mac\bin\Release\*.dll" target="lib\Xamarin.Mac20" /> <file src="iOS\bin\Release\*.dll" target="lib\Xamarin.iOS10" /> <file src="Android\bin\Release\*.dll" target="lib\MonoAndroid10" /> <file src="iOSClassic\bin\Release\*.dll" target="lib\MonoTouch10" /> </files>
The above ignores any portable class libraries.
Most .nuspec files specify the version number of the target framework but it is optional if your assembly works with all versions of that target framework. So if your target was lib\MonoAndroid this would mean it works with any version of Xamarin.Android.
You can specify the version with a set of numbers without a decimal point or you can specify it using decimal points. Without the decimal point NuGet will just take each number and turn it into a version by inserting a '.' between each digit.
In the above "MonoAndroid10" means "Android 1.0". This just means the
project's target framework
needs to be MonoAndroid version 1.0 or higher. The
version is specified in the
<TargetFrameworkVersion> element in the project
- MonoAndroid403 matches Android 4.0.3 and newer (ie API level 15)
- Xamarin.iOS10 matches Xamarin.iOS 1.0 and newer
- Xamarin.iOS1.0 also matches Xamarin.iOS 1.0 and newer
PCL NuGets with Platform Dependencies
PCL Profiles are limited in what .NET framework APIs they can access, and they certainly can't access platform-specific code. These 3rd-party links discuss different approaches for creating NuGet packages that use PCL and native APIs to provide compatibility for Xamarin and other platforms:
- How to Make Portable Class Libraries Work for You
- The Bait and Switch PCL Trick
- Creating a NuGet PCL that works with Xamarin.iOS
This external list of PCL Profiles with their NuGet target name is also a useful reference.
Some open-source examples that you can refer to:
- ModernHttpClient – Write your app using System.Net.Http, but drop this library in and it will go drastically faster (view source).
- Splat – A library to make things cross-platform that should be (view source).
- NGraphics - A cross platform library for rendering vector graphics on .NET (view source).