How-To: Android automatic build environment

After setting up your Android development environment, Developing an Android Application, and Created an Android Test environment it’s time to integrate your application and corresponding tests inside an automatic build environment. The steps to achieve this are:

  • Create an maven project structure.
  • Adding the specific Android setting to maven.
  • Build and run the application.
  • Build and run the tests.

This post isn’t about CI, it’s about how to setup an automatic build environment with use of Maven. I will use the Android application and corresponding tests which was developed inside my previous posts.

Create a Maven project structure

If you are brand new to it you may wish to first check out Maven in 5 minutes. Basically create a parent-child structure as shown below.

parent
|— Android
|          |— pom.xml
|— AndroidTest
|          |— pom.xml
|— pom.xml

In this post I’m using maven version 2.2.1.

Adding the specific Android setting to maven

I will be using the maven-android-plugin to build the Android application and Robotium to test the application. In this post I’m using maven-android-plugin version 2.5.2.

The parent project pom.xml must reference the maven-android-plugin, along with the android API level targeted, here I cite version 2.2 (api level 8 )

<plugin>
  <groupId>com.jayway.maven.plugins.android.generation2</groupId>
  <artifactId>maven-android-plugin</artifactId>
  <configuration>
    <sdk>
      <platform>2.2</platform>
    </sdk>
    <emulator>
      <avd>Device</avd>
      <wait>2000</wait>
    </emulator>
    <deleteConflictingFiles>true</deleteConflictingFiles>
    <undeployBeforeDeploy>true</undeployBeforeDeploy>
  </configuration>
  <extensions>true</extensions>
</plugin>

Build and run the application

  • To build the Android.apk, we need add a version of the android platform as a build path dependency in the Android project pom.xml file.
  • <dependencies>
      <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <scope>provided</scope>
      </dependency>
    </dependencies>
  • Add the goal into the Android project pom.xml to deploy the apk onto an Android emulator.
  • <plugins>
      <plugin>
        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
        <artifactId>maven-android-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
      </plugin>
    </plugins>
  • Specify the source directory.
  • <sourceDirectory>${project.basedir}/src</sourceDirectory>
  • Run the emulator
  • Run the maven command mvn clean install inside the Android project.
  • You should get the following result.




Build and run the tests

  • To run the tests inside the AndroidTest project, we need add the following dependency inside the AndroidTest project pom.xml file.
  • <dependency>
      <groupId>com.jayway.android.robotium</groupId>
      <artifactId>robotium-solo</artifactId>
      <version>1.4.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>android</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>android-test</artifactId>
      <scope>provided</scope>
    </dependency>
  • Add the source code of the Android application which it tests.
  • <dependency>
      <groupId>com.my.android</groupId>
      <artifactId>Android</artifactId>
      <version>${project.version}</version>
      <scope>compile</scope>
      <type>jar</type>
    </dependency>
  • Specify the source directory.
  • <sourceDirectory>${project.basedir}/src</sourceDirectory>
  • Run the emulator
  • Run the maven command mvn clean install inside the parent project.
  • You should get the following result.



Here’s the fully-configured project pom.xml files using ALL of the above:

parent
|— Android
|          |— pom.xml
|— AndroidTest
|          |— pom.xml
|— pom.xml

  1. Mike
    I played around with maven for android in the last few days and I find the configuration effort way to complicated compared to, for example, ant + ivy. What's your opinion?
  2. W.Elsinga
    I've been using maven on large scale projects, about 12 modules in each project, for about 5 years now and I must say the conventions are really a bless comparing to all tastes of gigantic ant build files I've seen. But on smaller scale projects you might be right. Personally I’m not familiar with Ivy, so I can’t make that comparison.

Leave a Reply

*

captcha *