Thursday, May 19, 2011

Using Apache Maven as a build manager for Android projects

Maven [1] is a powerful tool for managing and building Java projects. Naturally, it can also be used for Android projects. For this post, I will assume that the reader has some working knowledge of Maven. If not, the reader is referred to the Maven 5-minute guide [2].

Just like any Java project, an Android project that uses Maven as a build manager also has an underlying Project Object Model (POM). The POM is an XML file that contains all the information about the project. In case of Android, there's a Maven plugin [3] available that we will use.

The first order of the day is to install Apache Maven (v 2.2.1+). We don't need to explicitly install the Maven Android Plugin; That's a task that will seamlessly be handled by Maven for us, as we will later on see. After installing Maven, make sure that your ANDROID_HOME environment variable is set and $ANDROID_HOME/tools and $ANDROID_HOME/platform-tools are on your $PATH. If not, do so in your .bashrc file (in case of a Debian-based Linux distribution). Additionally, if you are an Eclipse user, make sure you install the Maven Integration for ADTs Eclipse plug-in [4].

Now that we have everything set-up, we add a pom.xml to our Android project. Following is a general template for pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
</project>

We will now add entities step by step to this POM (within the <project> </project> tags).

First and foremost, we need to configure the groupId, artifactId, version, packaging and name. Similar to:

<groupId>org.umit.icm.mobile</groupId>
<artifactId>icm-mobile</artifactId>
<version>1.0.0-test-maven</version>
<packaging>apk</packaging>
<name>UMITICMMobile</name>

Note that, the packaging is apk as opposed to the standard Java jar.

We now move on to dependencies. The most basic dependency for every Android project would obviously be the SDK. This dependency will be resolved from the Maven Central Repository. Assuming that we are using SDK 1.6:

<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>1.6_r2</version>
<scope>provided</scope>
</dependency>
</dependencies>

Finally, we add build information:

<build>
<finalName>${project.artifactId}</finalName>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<version>2.8.4</version>
<configuration>
<sdk>
<platform>4</platform>
</sdk>
<emulator>
<avd>16</avd>
</emulator>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
</plugins>
</build>

As mentioned earlier, the Maven Android Plugin will automatically be downloaded and installed. It's important to highlight that SDK platform is 4 because we are working with SDK 1.6. Likewise, the name of the AVD device is 16.

Now, we are ready to build our project and deploy it on the AVD. Using the terminal, we go to the project root where the pom.xml is located. To build the project we simply issue the command:

mvn install

To deploy the apk to the device we use:

mvn android:deploy

Our application is now ready to be used on the emulator.

Eclipse users, instead of using the terminal, can import the project through File > Import > Maven > Existing Maven Projects. It is important to mention that Eclipse will not recognize the gen/ folder as a source folder automatically. To remedy this, we right-click on the project folder, followed by properties and Java Build Path. From here we go to Source and then Add Folder. We select the gen/ folder and click on OK twice.


No comments:

Post a Comment