Mono for Android

Using C# to Develop for Android Devices

Brian Long Consultancy & Training Services Ltd.
May 2011

Page selection: Previous  Next

Note: This introductory tutorial was written for Mono for Android v1.0, as once sold by Novell. Since that time the team behind Mono for Android have left Novell and formed their own company, Xamarin and Mono for Android is up to v4.0 (at the time of editing this note). Consequently some aspects of this introductory article may be a little inaccurate with regard to the current version. For version 4 you should look at this updated version.

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 Novell®, 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#, 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 Novell’s Mono platform, meaning .NET programming skills can progress your application code base from solely targeting Windows (other than when working with Silverlight®) 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 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.

This is predicated on you having a copy of Visual Studio Professional or higher already 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.

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: 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 is Update 25 and the installer is called jdk-6u25-windows-i586.exe (76.6 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_25\bin to the PATH, and on 32-bit Windows you'd add C:\Program Files\Java\jdk1.6.0_25\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, so regardless of which platform versions you install, the current documentation package available will be listed as for API 11.

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. This is also done from within the SDK Manager in the Virtual Devices section.

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: if you later want to come back to edit your virtual devices, rather than running SDK Manager.exe, which immediately checks for new packages to install, you should run android.bat, which is located in C:\Program Files\Android\android-sdk-windows\tools by default. This will start the SDK manager and leave you on the Virtual Devices page. In fact, SDK Manager.exe is really a short cut for running:

android.bat update sdk

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:

Installing Mono for Android

Installing Mono for Android

Go back to the top of this page

Go back to start of this article

Previous page

Next page