Mono for Android Portable Libraries in VS
Thanks to Scott Hanselman's recent article on .NET's support for Portable Class Libraries (PCLs), there has been some renewed interested in using them for Mono for Android.
If you try to add a reference to a compiled PCL assembly, things should work, as VS simply checks that the version of mscorlib.dll referenced is the same as the MFA project (2.0.5). However, if you try to add a reference to a PCL project, you get this nasty dialog box:
The bad news is we don't ship any tooling for supporting these out of the box yet. The good news is it's trivial to do it yourself!
The portable libraries are installed in .NET's Reference Assemblies folder, which is here:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.0
If you explore around in that folder, you will see there are something called "Profiles" which specify the common assemblies for a given set of targets.
For example, "Profile2" targets .NET 4, Silverlight 4, and Windows Phone 7. This is the set that matches Mono for Android best, so we want to tell Visual Studio that it's okay for Mono for Android projects to use this set as well.
This is done by files in the "SupportedFrameworks" folder, specifically:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.0\Profile\Profile2\SupportedFrameworks
You can see there are already files for .NET 4, Silverlight 4, and Windows Phone 7:
In order to support Mono for Android, we just need to add a .xml file to this directory called "MonoAndroid,Version=v1.6+.xml":
The contents of the file are simply some metadata to tell Visual Studio about our target:
Once you have this file created, restart Visual Studio, and you should be able to add references to PCL projects in Mono for Android projects!
NOTE: Last time I checked, the Mono for Android profile is not an exact match for Profile2. There are a few rarely used methods that Mono for Android lacks, so you still have to be slightly careful about what you use.
If you try to add a reference to a compiled PCL assembly, things should work, as VS simply checks that the version of mscorlib.dll referenced is the same as the MFA project (2.0.5). However, if you try to add a reference to a PCL project, you get this nasty dialog box:
The bad news is we don't ship any tooling for supporting these out of the box yet. The good news is it's trivial to do it yourself!
The portable libraries are installed in .NET's Reference Assemblies folder, which is here:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.0
If you explore around in that folder, you will see there are something called "Profiles" which specify the common assemblies for a given set of targets.
For example, "Profile2" targets .NET 4, Silverlight 4, and Windows Phone 7. This is the set that matches Mono for Android best, so we want to tell Visual Studio that it's okay for Mono for Android projects to use this set as well.
This is done by files in the "SupportedFrameworks" folder, specifically:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.0\Profile\Profile2\SupportedFrameworks
You can see there are already files for .NET 4, Silverlight 4, and Windows Phone 7:
In order to support Mono for Android, we just need to add a .xml file to this directory called "MonoAndroid,Version=v1.6+.xml":
The contents of the file are simply some metadata to tell Visual Studio about our target:
<?xml version="1.0" encoding="utf-8"?> <Framework DisplayName="Mono for Android" Identifier="MonoAndroid" Profile="*" MinimumVersion="1.6" MaximumVersion="*" />
Once you have this file created, restart Visual Studio, and you should be able to add references to PCL projects in Mono for Android projects!
NOTE: Last time I checked, the Mono for Android profile is not an exact match for Profile2. There are a few rarely used methods that Mono for Android lacks, so you still have to be slightly careful about what you use.
Comments