NOW Hiring

Quick links

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. Overview

The build process of Android projects is handled by the Gradle build system. 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.

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 also use a wrapper script created by Gradle. This wrapper allows you to run a Gradle build without any additional installation from the command line.

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

1.2. Using Gradle on the command line

You can start your Gradle build also from the command line. For this execute the following command from the main project directory. This requires that Gradle is installed on the machine or that you use the Gradle wrapper (gradlew) to execute the build. The wrapper downloads Gradle if necessary. 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

# 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.3. 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.4. Defining dependencies and keepting 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 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 "". 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. About this website

6. Android Gradle build links

6.1. vogella GmbH training and consulting support


The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2016 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.