Setup Mono for Android on Windows - Tutorial

| Comments

After a short (3 weeks) adventure with Windows 8 I’ve decided I don’t want to struggle with the system (nor constantly switch back to ‘Desktop’ mode after another accidental switch to ‘no-longer-called-Metro-UI’) and so I came back to well known, liked, cozy Windows 7 x64. This is the system I’ve been using at work for a little more than last 3 years (basically since day one it RTM-ed) and the one I split time when developing at home - 50/50 with Mac OS X. I’m a convert from iOS church to the Android - the mobile OS that brought me to my knees with Samsung’s Galaxy S III (I’ll post about it on another occasion). And so I wanted to get at least a little bit familiar with developing for Android, however I wasn’t that much enthusiastic when I thought about a perspective of switching development environment to Eclipse and programming language I use on daily basis to Java. Fortunately Xamarin offers the tooling that allows developers to develop for Android (as well as iOS and Mac) using C# and Visual Studio (as an option).

Note: The best thing you can do is to head over to Xamarin - trials and after registering - run the setup. Mono for Android installer should automatically detect which of the prerequisites are missing from your system. However in my case (and as Google search results show I’m not the only one) - this didn’t work. Also - you need to have a paid version of Visual Studio (Express edition won’t suffice as it does not support 3rd party extensions). I also think - following step-by-step guide gives you a better understanding of the pieces of the puzzles involved, than clicking ‘next’ a few times in the installer

So here are the steps required to configure Mono for Android on Windows, with Visual Studio, to enable developing for Android mobile devices in C#:

  • Download Java Software Development Kit 6 Java SDK 6 - do NOT pick x64 version (even if you are on x64 version of Windows), go for x86 version. Also for Mono for Android 4.2.5 - that’s the one I’ve been using - Java SDK 6 is required (it won’t work with JDK 7 - although you can have both installed on your system side-by-side)
  • Download Android SDK - click on the ‘Download SDK Tools for Windows’ (ADT Bundle includes Eclipse, which is not the option I wanted to go for)
  • After Android SDK is installed Android SDK Manager will run. This allows installing different APIs (these depend on the Android version you would like to target). In my case I went for API 16 (Android 4.1.2), SDK tools, Android device emulator. Pick what you see fit.
  • Now it’s time to configure your AVD (Android Virtual Device) Choosing AVD option from Android SDK Manager menu

This is the emulator that will simulate a real physical device, when you will be testing your application written in C#, with Mono for Android (to deploy to real device you need to buy a licensed version from Xamarin). There are many different Android devices in the wild, differing in screen size, screen resolution, hardware capabilities, memory size, etc… The best way to setup AVD as so its configuration would reassemble a real device is to do a Google Search e.g. for ‘Android AVD configuration for Galaxy S III’. One thing to note here is that what is important is screen’s resolution, API version - which may vary, depending on which Android version you have installed on your handset. So for example for Galaxy S III bought in Poland in 2012 - 90% you’d get a 4.0 (Android Ice Cream Sandwich). However my handset has a custom ROM installed, which is based on the latest 4.1.2 (Android Jelly-Bean) and so I’d pick API16 rather than API15. You can check the version of the ROM (Android) installed on your phone by going to Settings->About device and checking value on ‘Android version’. So - the information Googled + info about your phone will tell you everything you need to know to successfully setup your AVD. First, switch to ‘Device definitions’ tab and then click ‘New device’ button. Set it up like so: Geekbeing's new device - Galaxy S III Next, based on this device - create your AVD, mine looks like that (Note: if later on, when trying to run your application on AVD - when it fails - change RAM size from 1024MB to 512MB - I haven’t figured out why, but it seems like AVD is unable to boot when setup with more memory than 512MB - and Visual Studio fails silently - waiting forever for Virtual Device to boot up):

Geekbeing's choice for Galaxy S III AVD

After everything has been installed, you can now run Visual Studio and verify whether everything’s setup properly. Begin your adventure by clicking File -> New Project and configure it like so: Geekbeing - new project, new adventure After the project has been created hit ctrl+shift+b to build the solution. If everything is installed and properly configured, you should end up with an output log similar to this one: Geekbeing - merry output log

Here are some materials to get you started: Mono for Android Tutorials and Guides for free, or should you prefer the book and have some dime to spend - Professional Android Programming with Mono for Android and .NET/C#, happy coding!