Home Tutorials Training Consulting Books Company Contact us


Get more...

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: https://jcenter.bintray.com/com/android/tools/build/gradle/

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'), 'proguard-rules.pro'
        }
    }
}

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: http://code.google.com/p/maven-android-plugin/ - Android-Maven Plug-in.

The Eclipse support of this plug-in is provided by the https://github.com/rgladwell/m2e-android - 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 "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. More information

For more information see the complete Maven Android Guide from Sonatype:

http://www.sonatype.com/books/mvnref-book/reference/android-dev.html - Android Application Development with Maven.

Another quick starting guide is available on the android-maven-plugin project side:

http://code.google.com/p/maven-android-plugin/wiki/GettingStarted - Getting Started with Android and Maven.

5. Android Gradle build links

Legal Privacy Policy Change consent