«

»

May
05

Using Maven for Android Development

As a habitual Maven user I’ve got used to the ease with which I can run through a build and test process and get a deployable artefact out at the end of it. I’ve been playing with Android for some time, and now I’m ready to do some serious development it is only natural that I want the same level of convenience. Here’s how to get going with Maven, based on chapter 14 of Sonatype’s Maven reference published here.

For clarity I’m going to cross-reference the section numbers as I go through, and remember this is what to do on a Mac.

14.2.1: I already had the latest Android SDK installed so the first thing I had to do was to set up an environment variable ANDROID_HOME – it wasn’t already present. Setting up a ‘global’ environment variable is easiest done by creating /etc/launchd.conf (if you don’t already have one) and then adding the line:

setenv ANDROID_HOME [location]

Replace [location] with the location of your SDK installation. You’ll need to reboot.

14.2.2 requires you to install the API Jars into your Maven repo. This is a sticky one because you’ll have to remember to do this each time you update your SDK with a new Android release. The instructions to download the Maven android deployer tell you to click on ‘Download Source’; you won’t find that – so click on ‘Download’ instead and you’ll get the choice of a .tar.gz file or a .zip. The Mac can deal with both but it’s more natural to go with the .tar.gz.

14.2.2.1 Before you go ahead and run mvn clean install it’s worth running up Eclipse and checking that your SDKs are up-to-date. I’m just saying! It’ll save some time later.

14.2.2.2 didn’t apply to me as I don’t (yet) have a remote repo.

14.2.2.3 I also skipped, you will too unless you are working strictly with a single API level.

14.2.3 If you don’t already have a settings.xml file, create one in ~/.m2 (if that’s you maven home) – you can find out more about this file from the maven reference – if you start with no file at all, what you need is:


<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
  http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <pluginGroups>
    <pluginGroup>
      com.jayway.maven.plugins.android.generation2
    </pluginGroup>
  </pluginGroups>
</settings>

14.3.1 If like me you started playing with Android without Maven (or in my case, before it was well supported) you can now convert your non-Maven project to a Maven project. Here, I’m picking up a copy of my WiFi Hotspot project (download here) and turning it into a Maven project, using the sample Maven pom.xml in this section as a starting point.

If you have cloned an existing Eclipse project, do not delete the .project and .classpath files, you’ll still need those. Create your pom.xml as a straight copy from the one shown in section 14.3.1; the key changes you’ll need to make to this are to the groupId, artifactId and version – use values that reflect your project.

You’ll also need to set the SDK version in two places – the dependency on the Android API (which is set to 2.1_r1) and the plugin configuration where ‘platform’ is 2.1. As my project was written for API level 8 (Android 2.2) I changed the first to ’2.2_r2′ and the second to ’2.2′. Finally, because the sample at Sonatype references an old version of the maven android plugin, I changed the plugin version from 2.2.3-SNAPSHOT to 2.8.4.

You’ll need to add any of your own dependencies. As my project was simple, I had none.

OK the next step (and we’ve still not imported the project into Eclipse) is to run a build. If you’ve got everything correct then all you’ll need to do is the vanilla build:

mvn clean install

If you got everything right, you’ll be rewarded by an apk file in your maven repo. My simple three-class project, with no tests, took 3 minutes the first time – including the Maven downloads – and subsequent builds took 7s.

The final step was to import my maven enabled Android project into Eclipse having previously removed it. I performed an import selecting ‘Existing Maven Projects’, I did get an error but the project opened correctly with no errors, was still recognised as an Android project, and would happily run in the emulator and deploy onto my phone over the USB cable.

This all deals with a pre-existing project. To be able to create a new Android project from scratch using an archetype you’ll need to add further plugins – see this archetypes project for details of some archetype plugins that build on the maven android plugin.

Of course once your project is using Maven you can start using continuous integration tools; the maven android plugin supports starting up the emulator, so with a properly prepped machine its not beyond the wit of man to use something like robotium and implement a full build and test cycle that runs independently on a build machine.