Brian Long Consultancy & Training Services
Ltd.
May 2011
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.
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.
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.
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.
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.
This involves a couple of steps.
Firstly, download the Android SDK installer from the Android SDK page.
Note: the SDK installer is available as a ZIP file to unzip where
you choose (currently called android-sdk_r10-windows.zip
at 31.3 MB)
or as an installer executable (currently called installer_r10-windows.exe
also at 31.3 MB). It is recommended to use the executable installer, as this puts
things in standard locations and also adds registry entries identifying where the
SDK is found and adds a Start Menu folder for the Android SDK Tools.
If you simply unzip the zipped version, these registry entries will not be present
and Mono for android will not be able to locate the SDK, meaning you will have to
take extra steps to help it out by setting a dedicated Mono for Android option in
the Visual Studio IDE.
Note: If the installer complains that the JDK cannot be found,
just click the installer's Back
button followed by the Next
button and it should find it on the second attempt.
Secondly, to pull down the main parts of the Android SDK, run the SDK Manager. If
you used the zipped version of the SDK installer you will need to locate and run
the program SDK Manager.exe
from wherever you unzipped the files to.
By default this will be C:\Program Files\Android\android-sdk-windows\SDK Manager.exe
,
or C:\Program Files (x86)\Android\android-sdk-windows\SDK Manager.exe
if running in 64-bit Windows.
Note: if you are running Windows Vista or Windows 7 you should
run the SDK Manager as an Administrator (use the menu item on the application shortcut's
context menu). This is because the installer defaults to installing the Android
SDK materials under C:\Program Files
and this is a protected directory
in recent versions of Windows. When the SDK Manager starts downloading the required
portions of the SDK it will need administrative privileges to avoid simply failing.
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.
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.
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:
ÿ+Break
then click
on Advanced system settings
ÿ+R
, type
systempropertiesadvanced
and press Enter
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:
C:\Program Files (x86)\Android\android-sdk-windows\tools
C:\Program Files (x86)\Android\android-sdk-windows\platform-tools
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.
Use the New...
button to create a new AVD, choose its attributes and
give it a name.
Pressing the Create AVD
button will create the virtual device and indicate
success with a message like this:
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.
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:
Go back to the top of this page
Go back to start of this article