Mono for Android

Using C# to Develop for Android Devices

Brian Long Consultancy & Training Services Ltd.
Jan 2012

Page selection: Previous  Next

Note: This introductory tutorial was written for Mono for Android v4.0, as developed and sold by Xamarin. Various aspects make it inappropriate as a resource for earlier versions of Mono for Android, for which you can use this earlier tutorial.

Executive Summary

Android devices have become very prominent over the last couple of years and represent a useful market sector to target for business applications. Traditional Android development is done in Java using the Eclipse IDE, but this is now no longer the only way to build Android apps.

With C# and Mono for Android® from Xamarin®, you have all you need to develop and debug applications for deployment to Android phones and tablets. This tutorial looks at getting started with the process of building Android applications with C# and Mono for Android v4, getting to understand the build process and what goes on in an Android application.

Contents

Introduction

Mono Mono for Android

As well as working with Microsoft’s .NET, you can also use C# to build applications for Xamarin’s Mono platform, meaning .NET programming skills can progress your application code base from solely targeting Windows (other than when working with Silverlight® or targeting Windows Phone 7) to working against Linux on various hardware platforms and also Mac OS X. Mono includes and enables various toolkits and libraries to support the UIs and technologies available on these platforms.

Mono for Android now extends this range of platforms to include Google's Android and includes API bindings and the required tooling to build and deploy Android applications. Mono for Android cuts out the need to learn the Java programming language and the Eclipse IDE, meaning you can retain your .NET programming skills and work in the familiar environment of Visual Studio.

Mono for Android was originally launched in April 2011. It offers several things to facilitate generating Android applications via the Mono platform:

Note: Whilst most .NET developers will be most comfortable working with Mono for Android from within Visual Studio on Windows, it is also possible to use the open source MonoDevelop environment on either Mac OS X or on Windows.

Getting started

Let's take a look at the process of getting set up for developing Mono for Android applications on Windows.

Mono for Android will either work in Visual Studio or you can work with the open source MonoDevelop IDE. If you favour Visual Studio, then you require a copy of Visual Studio Professional or higher installed. Mono for Android will not work with Visual Studio Express thanks to the Express versions not supporting the plug-in UI, which Mono for Android relies on to work.

Note: The following sections on installing the JDK and the Android SDK are technically unnecessary as the Mono for Android installer will check for them and install them for you you if found absent. In the case of the Android SDK it will also ensure that emulator images are created and ready for use. The additional information is included for information, in case you want to set things up manually.

Install Java Development Kit (JDK)

The Android SDK has various dependencies on the Java Development Kit for the Java Platform, Standard Edition 6 (JDK 6 or JDK SE 6) so this must be installed first.

You can download the JDK from Oracle's web site (click the JDK link).

Note: JDK 6 is no longer the latest version available from Oracle, but because of various changes in some of the Java tools in JDK 7, you should still install JDK 6.

Note: The JDK is a superset of the JRE (Java Runtime Environment). The JRE is insufficient; it's the JDK that must be installed.

Note: You must install the 32-bit JDK, even on 64-bit Windows. At the time of writing the current version of JDK 6 is Update 30 and the installer is called jdk-6u30-windows-i586.exe (78.9 MB).

Note: It is recommended to add the JDK's bin directory to the system PATH (see below for information on how to add to the system PATH). So, for example on 64-bit Windows with the JDK version mentioned above, you'd add C:\Program Files (x86)\Java\jdk1.6.0_30\bin to the PATH, and on 32-bit Windows you'd add C:\Program Files\Java\jdk1.6.0_30\bin.

The installation is quite straightforward.

Installing the JDK

Install Android SDK

This involves a couple of steps.

You can now choose parts of the SDK to install. You must install the Android SDK Tools and the Android SDK Platform-tools. In addition you can choose which SDK platform versions to install. Each platform version relates to a different version of Android. So, for example, SDK Platform 2.2, API 8 is the FroYo release of Android and SDK Platform 2.3.3, API 10 is the updated (maintenance release) Gingerbread release.

You can also choose to install a local copy of the API documentation if you wish. This allows access to the reference materials when not connected to the Internet.

Installing Android SDK packages

Note: the packages available for download by the SDK Manager are regularly updated, and so the revisions shown in the screenshot above may be different to what you see. Additionally, the documentation package available typically matches the latest available API release and so will be found as one of the options for the latest available API release (as in the screenshot above).

Note: Once you have an updated tools directory in your Android SDK directory you can also invoke the SDK Manager by launching android.bat, a batch file found in this tools subdirectory.

Add SDK folders to system PATH

To ensure you can readily run commands at later points you should add a couple of the Android SDK folders onto the system PATH.

To edit the system environment variables you need to go to the Advanced page of the System properties dialog. A couple of shortcut ways of getting there are:

Now click Environment Variables..., select PATH from the list of System variables, press Edit... and add additional semicolon separated paths. The paths to add are the tools and platform-tools folders under the SDK directory, so on a 64-bit Windows system with default installation that means adding these folders:

Configure one or more emulators

In order to test your applications on various potential device configurations you can set up emulators (Android Virtual Devices or AVDs) for different screen sizes, densities, Android platform and SD card size.

Note: while you can create AVDs yourself, the Mono for Android installer will also create various AVDs for you that are set up appropriately to test Mono for Android applications. This information is here just so you know the process of creating AVDs.

You can launch the Android Virtual Device Manager in various ways:

Installed Android emulators

Use the New... button to create a new AVD, choose its attributes and give it a name.

Creating an AVD

Pressing the Create AVD button will create the virtual device and indicate success with a message like this:

Successful AVD creation

You can see in the earlier screenshot that the Virtual Devices page tells you these emulator images are stored in %USERPROFILE%\.android\avd - in my case this expands to the C:\Users\Brian\.android\avd folder.

Note: It should be mentioned again that if you decide to install additional packages after invoking the SDK Manager through the android batch file, you will likely need to have launched it with administrative privileges. You can do so by running the batch file from a command prompt that was run as administrator.
An alternative in Windows 7 and (I think also) in Windows Vista is to press ÿ to bring up the Start menu, type in the android command, then press Ctrl+Shift+Enter to run the command as administrator (where just pressing Enter would run as the logged in user).

Note: You can start Android virtual devices (emulator images) from the SDK Manager but this is not recommended. It is best to let Mono for Android start the emulator as it ensures the emulator is launched with an appropriate partition size through a command line parameter.

Install Mono for Android

Lastly you need to install Mono for Android. You can get Mono for Android from the Store site. You can either purchase a copy or download the trial version. The Professional version costs $399 and gives you updates for a year. The trial version is fully functional other than being restricted to only working with the Android emulator; you cannot deploy to a physical device.

Either way, the installation is straightforward and full coverage is available on the Mono for Android documentation pages - remember that if you skipped the installation steps above, Mono for Android will spot this and happily install things for you. Indeed, over and above the JDK and Android SDK, the Mono for Android installer will install GTK# and MonoDevelop before installing Mono for Android.

Installing Mono for Android

Installer checking prerequisites

The installer screen above shows that it has found my Android SDK installation but needs to do some additional setup to match its requirements, such as creating emulator images for Mono for Android's use. On the other hand, if the Android SDK is not found then it will be pulled down and installed in %LOCALAPPDATA%\Xamarin\MonoForAndroid\AndroidSDK. You can see this happening below:

Installing Mono for Android prerequisites

The installation is nearly done here:

Installing Mono for Android

And now we're ready to go. You can either launch the MonoDevelop IDE with the huge button or press Close and launch Visual Studio yourself.

Installing Mono for Android

In Visual Studio you should choose Tools, Options... and then look at the Mono for Android settings to ensure that it can correctly see your Android SDK directory.

Mono for Android options in Visual Studio

Go back to the top of this page

Go back to start of this article

Previous page

Next page