Starting Android application development with Apache Maven

Despite of not being in widespread use in the Android world, Apache Maven is a great tool to develop Android applications. Maven has a lot of advantages relevant for Android application development:

  • It can be integrated on a continuous integration server, allowing continuous testing and nightly-build delivery
  • It provides a strict release process avoiding errors and inconsistencies. The resulting APK are ‘marketplace-ready’
  • It also provides a common way to build and package the applications
  • It supports modular-development allowing the integration of classes and resources from others projects

This post explores how to quickly start developing Android applications using Maven. It presents three archetypes allowing getting a running application in less than 2 minutes!

Introduction

The maven-android-plugin is a great Maven plugin to build Android application, however the initial configuration and layout are a little bit complex. Maven users are used to archetypes to quickly set up an already configured project with only one command line. An archetype is just a project template configured by the user used to generate a ready-to-use project.

The archetypes

akquinet has developed three Maven Archetypes relying on the maven-android-plugin. Those archetypes allows you to start an Android project in less than 2 minutes! The only prerequisites are:

  1. JDK 1.5+ installed as required for Android development
  2. Android SDK installed, preferably with all platforms, see http://developer.android.com/sdk/index.html
  3. Maven 2.2.1+ installed, see http://maven.apache.org/download.html (also support Maven 3.0+)

As the archetypes are deployed in Maven Central, you don’t have to install them manually, everything will be downloaded.
Once done, you can use the archetypes:

  • The ‘quickstart’ archetype generates a ready-to-be-deployed Android application
  • The ‘with-test’ archetype generates a multi-module project containing an application and a project
  • The ‘release’ archetype generates a multi-module project containing an application and a project testing this application (integration-tests) and configuring a release profile managing a zipaligned, signed and obfuscated artifacts (ready for the marketplace)

The quickstart archetype

This archetypes creates an dummy Android Application, ready to be deployed on an android device (real or emulator). To create the skeleton, just launch:

 mvn archetype:generate \   
-DarchetypeArtifactId=android-quickstart \
-DarchetypeGroupId=de.akquinet.android.archetypes \   
-DarchetypeVersion=1.0.4 \   
-DgroupId=your.company \   
-DartifactId=my-android-application 

You can also set the created ‘package’ with -Dpackage=your.company.android. By default, it uses the given ‘groupId’. You can also customize the Android platform. By default it uses 7 (Android 2.1). You can configure it using -Dplatform=....

The previous command will ask you for the desired project version:

~/tmp/test $  mvn archetype:generate \
:   -DarchetypeArtifactId=android-quickstart \
:   -DarchetypeGroupId=de.akquinet.android.archetypes \
:   -DarchetypeVersion=1.0.4 \
:   -DgroupId=your.company \
:   -DartifactId=my-android-application
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] org.apache.maven.plugins: checking for updates from ats.releases
[INFO] org.codehaus.mojo: checking for updates from ats.releases
[INFO] artifact org.apache.maven.plugins:maven-archetype-plugin: checking for updates from ats.releases
[INFO] ------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] [archetype:generate {execution: default-cli}]
[INFO] Generating project in Interactive mode
[WARNING] Archetype not found in any catalog. Falling back to central repository (http://repo1.maven.org/maven2).
[WARNING] Use -DarchetypeRepository=<your repository> if archetype's repository is elsewhere.
[INFO] Using property: groupId = your.company
[INFO] Using property: artifactId = my-android-application
Define value for property 'version': 1.0-SNAPSHOT: 
[INFO] Using property: package = your.company
[INFO] Using property: platform = 7
Confirm properties configuration:
groupId: your.company
artifactId: my-android-application
version: 1.0-SNAPSHOT
package: your.company
platform: 7
Y: Y
[WARNING] Don't override file /private/tmp/test/my-android-application/pom.xml
[INFO] ------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------
[INFO] Total time: 10 seconds
[INFO] Finished at: Thu Dec 09 11:56:08 CET 2010
[INFO] Final Memory: 16M/81M
[INFO] ------------------------------------------------------------

Once generated, the application is ready to be built and deployed. Start an android emulator (see here to start an emulator), or plug an Android dev phone, and launch:

cd my-android-application  
mvn clean install android:deploy

As you can see, the previous command has generated the APK and installed it on the device:

[INFO] [android:deploy {execution: default-cli}]
[INFO] /.../android/platform-tools/adb [install, -r, /private/tmp/test/my-android-application/target/my-android-application-1.0-SNAPSHOT.apk]
[INFO] 520 KB/s (15033 bytes in 0.028s)
	pkg: /data/local/tmp/my-android-application-1.0-SNAPSHOT.apk
Success

Now, check the applications of your device, you will see a ‘my-android-application’ application.

That’s it for the quickstart! Wasn’t it really quick? Let’s now look into the second archetype.

The –with-test archetype

The quickstart archetype creates a single project. This second artifact creates a multi-module project:

  • A parent pom
  • A module containing a dummy application
  • A module testing the dummy application (integration-tests)

To generate this structure, just launch:

mvn archetype:generate \   
-DarchetypeArtifactId=android-with-test \   
-DarchetypeGroupId=de.akquinet.android.archetypes \   
-DarchetypeVersion=1.0.4 \   
-DgroupId=com.foo.bar \   
-DartifactId=my-android-project \  
-Dpackage=com.foo.bar.android

The ‘package’ value is optional (by default use the groupId). You can also set the targeted Android platform with -Dplatform=x. By default, it uses 7 (android 2.1). Once generated, the application is ready to be built and tested. Start an android emulator, or plug an Android dev phone, and launch:

cd my-android-project
mvn clean install

This command builds the application, builds and executes the tests (on the device). If you look into the output, you will see:

[INFO] .../android/platform-tools/adb [shell, am, instrument, -w, com.foo.bar.android.test/android.test.InstrumentationTestRunner]

com.foo.bar.android.test.HelloAndroidActivityTest:.
Test results for InstrumentationTestRunner=.
Time: 1.981

OK (1 test)

Of course, if the tests failed, the build is aborted. This archetype is perfect for project regularly build on a continuous integration server checking the application after each commit.

The –release archetype

This archetypes is an extension of the with-test archetype but configuring the release process. Releasing is a topic addressed by maven (thanks to the maven-release-plugin). When we ask maven to perform a release, it creates ‘deliverable’ artifacts. In the Android case, it means an APK that can be published on the Android Marketplace.

This archetype configures the project to generate an APK:

To generate this project, just launch:

mvn archetype:generate \   
-DarchetypeArtifactId=android-release \   
-DarchetypeGroupId=de.akquinet.android.archetypes \   
-DarchetypeVersion=1.0.4 \   
-DgroupId=com.foo.bar \   
-DartifactId=my-android-project \  
-Dpackage=com.foo.bar.android

The ‘package’ value is optional (by default use the groupId). You can also set the targeted Android platform with -Dplatform=x. By default, it uses 7 (android 2.1). Once generated, the application is ready to be built and tested. Start an android emulator, or plug an Android dev phone, and launch:

cd my-android-project
mvn clean install

When you release your application, it will generate a signed, zipaligned and ProGuard-processed APK. You will have to add a profile to your ~/.m2/settings.xml containing the signing informations:

<profile>
  <id>android-release</id>
  <properties>
    <sign.keystore>/path/to/keystore</sign.keystore>
    <sign.alias>key alias</sign.alias>
    <sign.storepass>keystore password</sign.storepass>
    <sign.keypass>key password</sign.keypass>
  </properties>
</profile>

or directly pass those properties through the command line:

mvn release:prepare
mvn release:perform -Dsign.keystore=/path/to/keystore \
                    -Dsign.alias=key-alias \
                    -Dsign.storepass=keystore-password \
                    -Dsign.keypass=key-password

The archetype contains a test key store which MUST NOT BE USED IN PRODUCTION. However you can use it for testing:

mvn clean install -Prelease \
-Dsign.keystore=PATH_OF_THE_PROJECT/my-android-project/test-key.keystore \
-Dsign.alias=mykey \
-Dsign.storepass=testtest \
-Dsign.keypass=testtest

Be aware that Android cannot re-deploy artifacts using a different key, so be sure to undeploy all artifacts before running the release.

The used proguard configuration is a default configuration for Android applications, you might have to tune it to fit your application.

This archetype gives you a perfect skeleton to start developing an application intended to reach the Android Marketplace. As this configuration is quite cumbersome, relying on this artifact relieves a lot of pain.

Want to contribute?

The archetype was initially developed by the mobile development team of akquinet A.G. They was recently open-sourced (under the Apache License 2.0). If you want to contribute, just visit: the maven-android-archetype project. All contributions are welcome.

19 thoughts on “Starting Android application development with Apache Maven

  1. How to generate the IT classes and execute tests correctly? I need your help please

  2. Thank you for these archetypes.

    I only have some problems with the release archetype. Without further customizing it does not work.

    I have to change the in the of the application from android-classes to classes.

    Also the paths for the rt.jar and jsse.jar in the parent must be changed on Linux (at least for me). I have to remove the “/jre” from the path because java-home points already to the jre (I’m using a manually installed Java on my Linux computer).

    Also to run a release an scm and distributionManagement element must be configured in the parent pom. It would help other users if this would also be documented for the archetype.

    After this changes I can create a release with this archetype.

  3. IDEA is an amazing IDE, tons of refactoring options, its integration with
    modern frameworks (Guice, Freemarker…) is always up to date… Maybe the
    only thing I miss is eclipse Mylyn…
    Anyways, a personal license is pretty expensive…it would be a license for
    everyone attending or just one license for some lucky guy?

  4. Thanks a lot.

    I built using the maven-quickstart archetype with 1.0.6 but for some reason it put stuff in the package R.com.mycompany.project. I moved it and it worked, but since the apk has -1.0-SNAPSHOT I needed to manually install with adb.

  5. I possess been finding a lot of distinct ideas on that and discover it publish to be one of the much more enlightening on the topic. Hope we can increase the high quality of responses than which I have seen as many do not appear to be really pertinent.

  6. E:\workspace\android-archetype>call mvn archetype:generate -DarchetypeArtifactId=android-with-test -DarchetypeGroupId=de.akquinet.
    android.archetypes -DarchetypeVersion=1.0.4 -DgroupId=… -DartifactId=my-android-application
    [INFO] Scanning for projects…
    [INFO] Searching repository for plugin with prefix: ‘archetype’.
    [INFO] ————————————————————————
    [INFO] Building Maven Default Project
    [INFO] task-segment: [archetype:generate] (aggregator-style)
    [INFO] ————————————————————————
    [INFO] Preparing archetype:generate
    [INFO] No goals needed for project – skipping
    [INFO] Setting property: classpath.resource.loader.class => ‘org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader’.
    [INFO] Setting property: velocimacro.messages.on => ‘false’.
    [INFO] Setting property: resource.loader => ‘classpath’.
    [INFO] Setting property: resource.manager.logwhenfound => ‘false’.
    [INFO] [archetype:generate {execution: default-cli}]
    [INFO] Generating project in Interactive mode
    [WARNING] No archetype repository found. Falling back to central repository (http://repo1.maven.org/maven2).
    [WARNING] Use -DarchetypeRepository= if archetype’s repository is elsewhere.
    [INFO] ————————————————————————
    [ERROR] BUILD FAILURE
    [INFO] ————————————————————————
    [INFO] The defined artifact is not an archetype
    [INFO] ————————————————————————
    [INFO] For more information, run Maven with the -e switch
    [INFO] ————————————————————————
    [INFO] Total time: 2 seconds
    [INFO] Finished at: Thu Feb 10 21:50:37 MSK 2011
    [INFO] Final Memory: 8M/21M
    [INFO] ————————————————————————

  7. Hello,

    Could you try with -Dplatform=6 or 7, just to check if the IT works on newest versions ?

  8. Hi. This looks really useful. Trying the “-with-test” archetype with your example code is giving me a verify error when it attempts to run the tests. I’m using -Dplatform=4 Have you seen that before?

    [INFO] [android:internal-integration-test {execution: default-internal-integration-test}]
    [INFO] /home/builduser/tools/android/platform-tools/adb [shell, am, instrument, -w, com.foo.bar.test/android.test.InstrumentationTestRunner]
    INSTRUMENTATION_RESULT: shortMsg=com.foo.bar.test.HelloAndroidActivityTest
    INSTRUMENTATION_RESULT: longMsg=java.lang.VerifyError: com.foo.bar.test.HelloAndroidActivityTest
    INSTRUMENTATION_CODE: 0

Comments are closed.