Android Continuous Integration – Part 1
I’ve recently started working on a new Android project and my first task was to set up a continuous integration and testing environment. For a normal java project, this is not such a big deal. For Android however, I found out that’s not the case…or maybe it’s just me
So let’s start with the basics tools. First on my mind was Maven, which is very useful mostly for keeping the project organised, manage the external dependencies, take care of project’s lifecycle from compiling to releasing and deploying etc. You need to install Maven on your workstation and there are many tutorials on how to do that, which is beyond the scope of this blog post.
Maven knows Java projects very well. But for an Android project, you need to tell it what and how to do for different phases like compile and release. For example, when you release you want to be able switch the debug mode off, increment the project version, sign it with a release key, zipalign it etc. Maven knows how to do that thanks to a very useful plugin called android-maven-plugin. This plugin has several goals, ranging from packaging the .apk, running tests on the emulator to deploying the .apk and actually running the application etc.
You can find the documentation needed for using this plugin on their website. The basic usage is to create an Android project and then add a .pom file in which specify, among others, the use of the android-maven-plugin.
In this post I am providing a very small working Android-Maven project with two modules (one for the app and one for the tests). Also I’m gonna tell you how I added Robotium for integration tests and Robolectric for unit tests as well my view regarding which is better later on. There are a few glitches that I’ve struggled with while making this project work, so it might be useful to read on and save some time…if not, jump directly to the source here. Disclaimer: if the project doesn’t work out of the box on your machine, it probably means you have to tweak things here and there like explained below.
Most of us mortals use Eclipse to write Android code because of the ADT plugin. Eclipse Indigo comes bundled with the m2e plugin which is used for enabling the Maven nature on a project. On older Eclipse versions you can install this from Eclipse Marketplace. If you add a .pom file to your normal Android project and you have m2e installed inside Eclipse, then all you have to do is right-click the project and select Configure > Convert to Maven project. So instead of the usual way of managing project dependencies by creating a
libs folder inside your project, manually adding the needed .jars in there (and keeping track of versions) and tell Eclipse to add those jars in the build classpath, you enable Maven to take care of those dependencies using a POM file.
The problem I’ve faced right away was that m2e did not recognised the android-maven-plugin by saying something weird like Plugin execution not covered by lifecycle configuration. There is an Android Maven Configurator plugin that makes m2e work with Android. However, I could not fix it by pressing Discover new m2e connectors because it kept saying it can’t find any connectors. So after googling and stackoverflowing I found out that you need to go to Eclipse > Preferences > Maven > Discovery and press Open Catalog. Then you’ll find the Android Connector as the first listing. You install it and it works. Mostly…
Download the sample code or clone it with git from https://github.com/CodeAndMagic/AndroidMagic