Lap around the new Mono for Android 4.2 release

<p>Today Xamarin released a new version of Mono for Android, the C# tooling for building apps on Android devices. The new release features a ton of new and old stuff that got improved. There's quite a few things in there that will make many developers happy for sure. </p> <p>In this post I will give you a very short runthrough of what's there and why I think it is good to have.</p><!--more--><h2>The new Mono for Android designer</h2> <p>The first feature I'd like to talk about is the new designer that has been added to the Mono for Android tools. This designer was one of the things I wanted since the beginning. You can edit the xml files for the layouts by hand, but it's a pain in the behind. Later there was a designer in Eclipse, but that one sucks too much to be of any serious use. So I was very happy that Xamarin has created a designer that integrates nicely into Visual Studio and does a good job at designing layouts for your apps.</p> <p><a href="http://fizzylogic.azurewebsites.net/wp-content/uploads/2012/05/image.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://fizzylogic.azurewebsites.net/wp-content/uploads/2012/05/image_thumb.png" width="707" height="397"></a></p> <p>The designer has a ton of options. You can select the device you're designing for, the screen you're designing for and the operating system version you're designing for. Last but least you can also configure which orientation the screen has. This maximizes the comfort for the developer who is designing the app. </p> <p>If this doesn't make you want to program C# on Android, I know another good reason to start developing C# for Android. The property editor for the screen layouts is waaay better than the clunky, broken, irritating property editing experience eclipse offers.</p> <p align="center"><a href="http://fizzylogic.azurewebsites.net/wp-content/uploads/2012/05/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://fizzylogic.azurewebsites.net/wp-content/uploads/2012/05/image_thumb1.png" width="124" height="244"></a> <a href="http://fizzylogic.azurewebsites.net/wp-content/uploads/2012/05/image2.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://fizzylogic.azurewebsites.net/wp-content/uploads/2012/05/image_thumb2.png" width="273" height="244"></a> <a href="http://fizzylogic.azurewebsites.net/wp-content/uploads/2012/05/image3.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://fizzylogic.azurewebsites.net/wp-content/uploads/2012/05/image_thumb3.png" width="131" height="244"></a></p> <h2>Better support for API targeting</h2> <p>In the past you could build Mono for Android apps with a platform setting configured to Android 2.2, this would produce an app that can be run on Android 2.2, but not lower. Even if you set the target version in your manifest to API level 8, the app would in most cases not start on device running that API level. The only way around this problem is to lower the target platform setting on the properties page of your project and skip on some of the useful features that a higher API level has to offer.</p> <p>To fully understand why this is a problem, let me tell you a littlebit about the WebView widget on Android. This widget can be used to display webpages in your application. It can handle both HTTP and HTTPS traffic. But the latter has some specific issues. If you happen to be running Android 2.1, the default behavior for handling HTTPS traffic is as follows. If the site has a certificate, the device doesn't trust, the request is aborted. Simple as that. There is no way around it. If you target Android 2.2, you can add a so called WebViewClient to this widget, which allows you to handle this situation much more gracefully by overriding OnSslErrorReceived and calling handler.Proceed() inside this method. Funny enough, the trick works for Android 2.1 devices too, but you need your app to be build with SDK level 8 (Android 2.2) and the manifest set to API level 7 (Android 2.1). </p> <p>With the new version you can do this and the above will work fine. Keep in mind though that although you can do this with the new Mono for Android 4.2 tooling, you need to make sure that the Java classes you use, do exist on the target API level. Otherwise you end up getting a load of Java errors.</p> <h2>Java API binding generation</h2> <p>The last feature I'd like to talk about is Java API binding generation. Mono for Android at the heart is the Mono framework with a bunch of Java API bindings that allow you to invoke Java code from C#. This is the way your C# activities can call into OS functionality. </p> <p>In previous versions you could write your own bindings by hand. I've done that too, but it's a painful process and there's no guarantuee that it will work without problems. In the new version of the Mono for Android tooling you can do the same thing without having to worry about delegates, IntPtrs and method signatures. Instead Xamarin created a custom project type that you can use to generate the correct bindings for you. Excellent stuff when you want that extra Java library in your app.</p> <p><a href="http://fizzylogic.azurewebsites.net/wp-content/uploads/2012/05/image4.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://fizzylogic.azurewebsites.net/wp-content/uploads/2012/05/image_thumb4.png" width="314" height="218"></a></p> <p>Not only did they improve the Java to C# binding, they also improved the way C# code is exposed to Java. This means that you can now export arbitrary C# members so that they are made available in Java. It is all automatically translated into the correct Java components. What that means? It means that things like java.lang.ISerializable can be implemented on your C# class. Imagine serializing a C# object in Java land so it can be sent to other Java activities etc. Cool stuff!</p> <h2>The bonus section</h2> <p>And to top it all off, here's the bonus section with other stuff that is fixed:</p> <ul> <li>The shared runtime shrunk by 50% which means smaller binaries, which means targetting more devices that have less space available to store apps in.</li> <li>Support for Android 4.0 (Icecream sandwich)</li> <li>Support for the x86 emulator. This is great since the x86 emulator is faster than the ARM emulator for Android. Great for giving presentations. And who knows, some day you may end up running Mono for Android apps on a x86 Android device.</li></ul> <p>I personally think that this is one of the best releases Xamarin has done so far. Now if you will excuse me, I've got some libraries to recompile <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-winkingsmile" alt="Knipogende emoticon" src="http://fizzylogic.azurewebsites.net/wp-content/uploads/2012/05/wlEmoticon-winkingsmile.png"></p>

Adding async/await support to your Mono for Android projects

<p>Visual Studio 11 beta is taunting you, with its new support for async and await keywords. All you have been wanting to do is use those keywords to finally clean up the mess you produced building an API that is completely async. Sadly, there's no way you are going to be able to use the keywords until the third party tools, you are using are updated or your boss tells you that Visual Studio 11 is RTM and can be used in the company.</p> <p>This is the scenario where I am in right now, well for the first part that is. The second part is not a problem, since at the office we've already ditched Visual Studio 2010 and moved on to Visual Studio 11 for its new build features and TFS server features. However if you're like me, doing Mono for Android development you might want to read on. </p><!--more--><h2>What is async/await?</h2> <p>For those developers that read this post and think: "What the .. is async/await?". These new keywords have been added to the C# 5.0 spec to make working with asynchronous tasks alot easier. Take for example, the following code:</p>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Program
{
static void Main(string[] args)
{
var fibonnaciTask = FibonnaciAsync(100).ContinueWith(task =>
{
Console.WriteLine("Outcome: {0}",task.Result);
});
fibonnaciTask.Wait();
}
private static Task<int> FibonnaciAsync(decimal n)
{
return Task.Factory.StartNew(() => Fibonacci(n));
}
private static int Fibonacci(decimal n)
{
if(n == 0)
{
return 0;
}
if(n == 1)
{
return 1;
}
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}

<p>It starts a task, waits for it to complete and executes some finalizer code to process the results of the task. This stuff is very common when you are writing async API calls with the C# 4.0 tooling. This gets messy very quickly, as the following piece of junk code demonstrates:</p>