I’ve been playing with building Android applications using Maven and the maven-android-plugin. There’s some excellent documentation out there, but I made some notes as I followed it.

Requirements

Ensure that you have the following installed:

  • Android SDK
  • Maven2
  • Eclipse with m2eclipse

This guide assumes you are using a Debian-flavoured version of Linux, such as Ubuntu.

Add some environment variables

Open ~/.bashrc and add the following line

export ANDROID_HOME=/usr/local/share/applications/android-sdk-linux_86/

Replace the /usr/local/… with the location of the Android SDK.

You will need to re-load “.bashrc” for this change to be effect. In a terminal you can run the following command:

. ~/.bashrc

Before Eclipse and other desktop applications are able to see the variable you will need to log in again.

Install maven-android-sdk-deployer

Download the maven-android-sdk-deployer and unzip. Once within the folder run the following command:

mvn install -P 2.1

This will install the dependencies for the Android 2.1. You can install other version - for more information view the README.

Set up the project structure

Create a folder for source code

mkdir -p android-example/src/main/java

Place all of your source code, or some code from an existing project into this folder.

And enter the following into android-example/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>
<groupId>com.owengriffin.android</groupId>
<artifactId>android-maven-example</artifactId>
<name>Android Example</name>
<version>0.1.0</version>
<packaging>apk</packaging>

<build>
	<sourceDirectory>src/main/java</sourceDirectory>
	<plugins>
		<plugin>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.5</source>
				<target>1.5</target>
			</configuration>
		</plugin>
		<plugin>
			<groupId>com.jayway.maven.plugins.android.generation2</groupId>
			<artifactId>maven-android-plugin</artifactId>
			<version>2.3.3</version>
			<configuration>
				<sdk>
					<platform>7</platform>
				</sdk>
				<deleteConflictingFiles>true</deleteConflictingFiles>
			</configuration>
			<extensions>true</extensions>
		</plugin>

	</plugins>

</build>

<dependencies>
	<dependency>
		<groupId>android</groupId>
		<artifactId>android</artifactId>
		<version>2.0.1_r1</version>
		<scope>provided</scope>
	</dependency>
</dependencies>
</project>

Modify the groupId and artifactId to be something a little more appropriate.

Go!

When running mvn install command you should see an .apk file appear in the target folder. You can install this apk file to the emulator using mvn android:deploy.

Eclipse Plugin

Assuming you have m2eclipse installed you can still use the Android SDK integration.

Add the following URL as an Eclipse update site and install “Maven Integration for Android Development Tools”

http://m2eclipse-android-integration.googlecode.com/svn/trunk/com.byluroid.eclipse.maven.android.update/

For more information, and more verbose instructions read the Getting Started Guide