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

1. Gradle for building Android applications

1.1. Using Gradle for Android apps

By default, Android projects are handled by the Gradle build system. If you create a new project in Android studio, the Gradle build scripts are automatically created. Android studio provides the Gradle runtime, hence no additional installation is required.

If you press the run button in Android Studio, it triggers the corresponding Gradle task and starts the application.

You can also run Gradle via the command line. To avoid unnecessary local installation, Gradle provides a wrapper script which allows you to run Gradle without any local installation.

You find the available versions of the Android Gradle plug-in under the following URL:

1.2. Conversion process from source code to Android application

The Java source files are converted to Java class files by the Java compiler. The Android SDK contains a tool called dx which converts Java class files into a .dex (Dalvik Executable) file. All class files of the application are placed in this .dex file. During this conversion process redundant information in the class files are optimized in the .dex file. For example, if the same String is found in different class files, the .dex file contains only one reference of this String.

These .dex files are therefore much smaller in size than the corresponding class files.

The .dex file and other resources, e.g., the images and XML files, are packed into an .apk (Android Package) file. The program aapt (Android Asset Packaging Tool) performs this step.

The resulting .apk file contains all necessary data to run the Android application and can be deployed to an Android device via the adb tool.

As of Android 5.0 the Android RunTime (ART) is used as runtime for all Android applications. ART uses a combination of Ahead Of Time and _Just In Time _ compilation. During the installation of an application on an Android device, the application code is translated into machine code.

The dex2oat tool takes the .dex file created by the Android tool chain and compiles that into an Executable and Linkable Format (ELF file). This file contains the dex code, compiled native code and meta-data. Keeping the .dex code allows that existing tools still work.

1.3. Using Gradle on the command line

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 code

You can start your Gradle build via the command line. Here is an overview of the important Android Gradle tasks:

Table 1. Android Gradle build targets
Command Description

./gradlew build

build project, runs both the assemble and check task

./gradlew clean build

build project complete from scratch

./gradlew clean build

build project complete from scratch

./gradlew test

Run the tests

./gradlew connectedAndroidTest

Run the instrumentation tests

To see all available tasks, use the gradlew wrapper command.

gradle build
# alternatively speedup second grandle build by holding it in memory
# gradle build --daemon

This command creates in the build folder the output of the Gradle build. By default, the Gradle build creates two .apk files in the build/outputs/apk folder.

To build and start your unit tests on the JVM use the following command.

gradle test

To build and start your instrumented tests on your Android device use the following command.

gradle connectedCheck

1.4. Removing unused resources and Java classes via resource shrinking

The Gradle build system for Android supports resource shrinking at build time. This automatically removes resources that are unused from the packaged application. In addition to that, this also removes unnecessary resources from libraries you are depending on. This can hugely reduce the size of your application.

To enable resource shrinking, update your build file similar to the following snippet.

android {

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

1.5. Defining dependencies and keeping the version external

A good practice is to define the version of your library dependencies outside the dependencies closure for better maintenance.

ext {
    // App dependencies
    junitVersion = '4.12'
    mockitoVersion = '1.10.19'
    powerMockito = '1.6.2'
    hamcrestVersion = '1.3'

dependencies {
    // Dependencies for local unit tests
    testCompile "junit:junit:$junitVersion"
    testCompile "org.mockito:mockito-all:$mockitoVersion"
    testCompile "org.hamcrest:hamcrest-all:$hamcrestVersion"
    testCompile "org.powermock:powermock-module-junit4:$powerMockito"
    testCompile "org.powermock:powermock-api-mockito:$ext.powerMockito"
If you put the ext closure into the root build file, you can access its properties for example with '$rootProject.ext.junitVersion'.

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 <filename class="directory">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 than 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 "". 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=""
 <name>Maven Example</name>

      <!-- platform or api level (api level 4 = platform 1.6) -->

Switch to the command line and enter the following command:

mvn3 android:deploy

4. More 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.

5. Android Gradle build links