Support free tutorials



vogella training Training Books

Building Android Apps with Apache Maven - Tutorial

Lars Vogel

Version 0.3

07.03.2012

Android Maven

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


Table of Contents

1. Building Android application with Gradle
1.1. Gradle for building Android applications
1.2. Purpose of the Gradle build system
1.3. Required setup for using Gradle
1.4. Adding a Gradle file for your Eclipse based Android project
1.5. Importing an Eclipse based Android project into Android Studio
2. Building Android applications with Maven
3. Tutorial: Building Android Applications with Maven
4. Move information

1. Building Android application with Gradle

1.1. Gradle for building Android applications

The build process of Android projects is handled by the Gradle build system. Android projects come in two flavors, the first set of projects uses the legacy project structure used by the Eclipse ADT tooling which was used until 2013, the second set of project uses the new Gradle build structure.

Gradle can be configured to support both formats, the Eclipse project structure as well as the Gradle project structure.

At this point in time the Eclipse tooling for Android development only supports the Eclipse based project structure hence if you want to use either Android Studio or Eclipse for your project development you need to keep the Eclipse project structure.

1.2. Purpose of the Gradle build system

The Gradle build system is designed to support complex scenarios in creating Android applications:

  • Multi-distribution: the same application must be customized for several clients or companies

  • Multi-apk: supporting the creation of multiple apk for different device types while reusing parts of the the code

1.3. Required setup for using Gradle

If you create a new project in Android studio, the Gradle build scripts are automatically created. Android studio wraps the Gradle runtime hence no additional installation is required. You can also use a wrapper script created by Gradle which allows you to run a Gradle build without any additional installation from the command line.

If you create a new project in Android studio, the Gradle build scripts are automatically created. Android studio wraps the Gradle runtime hence no additional installation is required. You can also use a wrapper script created by Gradle which allows you to run a Gradle build without any additional installation from the command line.

1.4. Adding a Gradle file for your Eclipse based Android project

To enable a Gradle build for your Eclipse based Android project add the following build.gradle to the root of your project.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
    }
}
apply plugin: 'com.android.application'


android {
     lintOptions {
          abortOnError false
      }

    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
      targetSdkVersion 22
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
} 

1.5. Importing an Eclipse based Android project into Android Studio

Once you added a valid Gradle file to your Eclipse based Android project you can import it into Android Studio, via FileImport Project and by selecting the project folder with the Gradle build file.

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.

The Eclipse support of this plug-in is provided by the m2e project.

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