Support free tutorials:











vogella training Training Books



Building Android Apps with Apache Maven - Tutorial

Lars Vogel

Version 0.3

07.03.2012

Revision History
Revision 0.1 24.08.2011 Lars
Vogel
Created
Revision 0.2 - 0.3 24.02.2012 - 07.03.2012 Lars
Vogel
bugfixes and enhancements

Android Maven

This tutorial describes how to build Android applications with Apache Maven and the "android-maven-plugin".


Table of Contents

1. Building Android Applications
1.1. Android SDK and ADT
1.2. Required setup for using Gradle
2. Building Android applications with Maven
3. Tutorial: Building Android Applications with Maven
4. Move information

1. Building Android Applications

1.1. Android SDK and ADT

The Android SDK provides tooling for the command line to create and build Android projects. The Android Developer Tools (ADT) offer the same functionality for Eclipse. In Eclipse you can manually export Android applications via the Eclipse wizard.

The Android tools team is currently working on Gradle as the new build system for Android application. At the moment it is not planned to offer Android Gradle support for ADT but you can configure the Gradle build script to compile your Android application created by ADT.

Gradle is an advanced build management system based on Groovy. A project describes its Gradle build in a file called build.gradle located in the root folder of the project.

Gradle supports the management of project dependencies (libraries). For this it supports existing Maven and Ivy repositories for retrieving dependencies. This allows a reuse of artifacts of other build systems.

Gradle supports Maven and Apache Ivy as repositories for dependencies.

Tip

This area is still evolving fast. See the Android Gradle Plugin User Guide for new information.

1.2. Required setup for using Gradle

Building Android outside Eclipse involves using the command line or shell.

Note

The note You should be familiar with using a shell in case you want to be able to perform this tutorial.

Make sure that the tools and platform-tools folder of the Android SDK installation directory are part of your PATH environment variable.

2. Building Android applications with Maven

The android-maven-plugin plug-in allows to build Android applications via Maven.

The webpage of this maven plug-in is located under: Android-Maven Plug-in.

You only have to install Maven, write a correct pom.xml file and issue the commands to Maven to build, install and run your application.

The following will build your Android application via Maven.

mvn3 clean install 

This will create the application and places the .apk file in the target folder.

If you want to install the application via Maven on your Android device, you can use the following command.

mvn3 android:deploy 

If more then one device is available you can specify the relevant device in your pom.xml. Maven can also start and stop an Android virtual device automatically for you.

You can also start the application via Maven.

mvn3 android:run 

3. Tutorial: Building Android Applications with Maven

Create a new Android project called "de.vogella.android.build.firstmaven". The actual content of the repository is not important as we are only using this project to create a working build for Maven.

Create the following "pom.xml" file in your directory. Make sure that artifactId is set to your project name.

<?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.example</groupId>
 <artifactId>de.vogella.android.build.firstmaven</artifactId>
 <version>1.0.0-SNAPSHOT</version>
 <packaging>apk</packaging>
 <name>Maven Example</name>

 <dependencies>
  <dependency>
   <groupId>com.google.android</groupId>
   <artifactId>android</artifactId>
   <version>2.2.1</version>
   <scope>provided</scope>
  </dependency>
 </dependencies>
 <build>
  <finalName>${project.artifactId}</finalName>
  <sourceDirectory>src</sourceDirectory>
  <pluginManagement>
   <plugins>
    <plugin>
     <groupId>com.jayway.maven.plugins.android.generation2</groupId>
     <artifactId>android-maven-plugin</artifactId>
     <version>3.1.1</version>
     <extensions>true</extensions>
    </plugin>
   </plugins>
  </pluginManagement>
  <plugins>
   <plugin>
    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
    <artifactId>android-maven-plugin</artifactId>
    <configuration>
     <sdk>
      <!-- platform or api level (api level 4 = platform 1.6) -->
      <platform>8</platform>
     </sdk>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project> 

Switch to the command line and enter the following command:

mvn3 android:deploy 

4. Move information

For more information see the complete Maven Android Guide from Sonatype: Android Application Development with Maven.

Another quick starting guide is available on the android-maven-plugin project side: Getting Started with Android and Maven