Android Development using Maven


If you are working alone in creating an Android application, there seems to be no problem at all using your IDE and ADT plugin to automatically build everything for you but if there are other people working on several modules in a particular Android application, you will need a better way to build your  application.

I’ve actually just started learning about this Maven-Android plugin. There are several reasons why I think using Maven will speed up the development cycle:

  • Resources can be stored in central repository. In our case we are using Artifactory but I’m curious how easy it is to use Apache Archiva.
  • You can automatically sign the APK with your keystore.
  • You can dynamically include resources in the build.
  • It will be very easy to integrate your build into any CI server such as Hudson or Jenkins.
  • You can automatically run unit tests after the build.
  • It will be easier to do a release using the maven:release plugin
  • If someday, Android Market will open up an API where developers can automatically publish the app, Maven is flexible enough to support this.
  • Automatically obfuscate your code using Proguard.

Installing Maven

You can download maven (as of this writing, the latest version is 3.0.3) here:

http://maven.apache.org/download.html

Instructions on how to install Maven for Windows, Linux or Mac is on the same link above.

Creating your Android project

Before we create our application, make sure that you have the Android SDK installed properly. The easiest way to create a project using Maven is to use the archetype from:

https://github.com/akquinet/android-archetypes/wiki/Android-release-archetype

This archetype is already in the Maven Central repository so you don’t need to install or configure anything aside from running the mvn command. We will be using the following command to generate our project.


mvn archetype:generate \
    -DarchetypeArtifactId=android-release \
    -DarchetypeGroupId=de.akquinet.android.archetypes \
    -DarchetypeVersion=1.0.5 \
    -DgroupId=com.mlst.mavenandroid \
    -DartifactId=my-android-project \
    -Dpackage=com.mlst.mavenandroid

Most of them are self-explanatory but if you are not familiar with them, try to read Maven’s guide to naming conventions for groupId, artifactId and version here:

http://maven.apache.org/guides/mini/guide-naming-conventions.html

You will notice that maven will ask you what platform version you want to use in your application. In our case, we will just leave it as the default which is platform 7 or android 2.1. If you want to know more about the Android Platform Levels, take a look at this post:

http://developer.android.com/guide/appendix/api-levels.html

After running the command, it will create a directory my-android-project which contains the following files:



osiris:my-android-project marc$ ls -l
total 32
drwxr-xr-x  11 marc  staff    374 Aug 21 21:31 my-android-project
drwxr-xr-x   8 marc  staff    272 Aug 21 21:31 my-android-project-it
-rw-r--r--   1 marc  staff  10276 Aug 21 21:15 pom.xml
-rw-r--r--   1 marc  staff   1258 Aug 21 21:15 test-key.keystore

my-android-project - will contain your project sources, proguard configuration, AndroidManifest.xml and other resources.

my-android-project-it - this is the integration test project which can be used to run instrumentation tests on the emulator or a device.

You should replace test-key.keystore with your own keystore, either self-signed or purchased from providers. For more information about creating your own self-signed keystore, check this post:

http://developer.android.com/guide/publishing/app-signing.html

Let Maven do the Magic

Now you can compile, install and test your app by just using a very simple command:

osiris:my-android-project marc$ mvn clean install

This will automagically install the two APKs (my-android-project and my-android-project-it) into your device or emulator (if it is running). You will notice the following logs which shows the command that installs and executes integration tests:


[INFO] /Applications/Dev/android-sdk-mac_86/platform-tools/adb [install, -r, \
/Users/marc/projects/my-android-project/my-android-project/target/my-android-project-1.0-SNAPSHOT.apk
[INFO] 4275 KB/s (25755 bytes in 0.005s)
    pkg: /data/local/tmp/my-android-project-1.0-SNAPSHOT.apk
Success
[INFO] /Applications/Dev/android-sdk-mac_86/platform-tools/adb [install, -r, \
/Users/marc/projects/my-android-project/my-android-project-it/target/my-android-project-it-1.0-SNAPSHOT.apk
[INFO] 1183 KB/s (24599 bytes in 0.020s)
    pkg: /data/local/tmp/my-android-project-it-1.0-SNAPSHOT.apk
Success
[INFO]
[INFO] --- maven-android-plugin:2.8.4:internal-integration-test (default-internal-integration-test) @ my-android-project-it ---
[INFO] /Applications/Dev/android-sdk-mac_86/platform-tools/adb [shell, am, instrument, -w, \
com.mlst.mavenandroid.test/android.test.InstrumentationTestRunner

com.mlst.mavenandroid.test.HelloAndroidActivityTest:.
Test results for InstrumentationTestRunner=.
Time: 0.432

OK (1 test)

Since the integration test application is empty right now, you will not see anything being executed at all on your device.

comments powered by Disqus