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:

<?xml version="1.0" encoding="utf-8"?>
<Framework DisplayName="Mono for Android"
  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.


Slodge said…
Perfect timing! Thank you! Well, maybe a day late... I was playing with this exact code yesterday on droid and touch. Will try again tonight. Thank you heaps for posting this. @slodge
Slodge said…
I think I've followed the post perfectly, but I can't get this working. I'm wondering if there's anything I have to do in order to tell the projects to use Profile2? Or if you have any other ideas? Sorry - but I've added the file, restarted the machine and I still get the error message :/ @slodge
Shady M. Najib said…
Is it Profile2 or Profile104 folks? I've used Profile104 (according to SLodge's post, but I get an error telling me "There is no available functionality that is portable between the frameworks you have selected". Also, Jpobst ends his post with a warning of the Mono for Android profile; not being an exact match for Profile2.. Any new updates about that? (Note: I'm using VS2010)

Popular posts from this blog

Hack Week 3

Mono in Visual Studio 2010

Introducing Pinta