Automatically starting the emulator in your Android build

In the beginning writing apps for Android is easy. Then the usual things creep in. You start getting beyond Hello World examples and answers on mailing list, you begin refactoring code and features and you really need to make sure it runs on all platforms. Luckily there are helpers around. For the testing side of things the whole test suite around instrumentation tests is really great (although I wish they used testng and not junit as the base) and now this is finally really well documentation on the site and not just in the samples code somewhere. However if you really want to use these you have to run your build against a device or an emulator (or better both). And thats where the Maven Android Plugin comes in handy.

One of the features I have implemented recently is the functionality to start and stop an emulator. To use that feature you are best off adding your emulator configuration to your pom.xml file. An example could be


The avd parameter would be the name of an Android Virtual Device (AVD) that you have created with the android tool from your SDK. The wait parameter is the millisecond the plugin should wait for the emulator to start up. The options value is passed through to the emulator command and allows you to add any further desired settings. With this setup you can start the emulator with the simple command

mvn android:emulator-start

and if you want to stop the emulator you can use (surprise..)

mvn android:emulator-stop

Thats all fairly straight forward but there is a lot more power to this. Imagine you want the emulator to always be up and running when you start your build because it uses some wicked instrumentation tests and they run on the emulator/device as you all know. Well you can add an execution of the emulator start goal to the build and bind it to the initialize phase by adding this snippet under the plugin configuration


The first time you run this it would start your emulator as the first thing in your build and be ready for those tests. Well at least provided your machine is fast enough for the wait time specified. The second time the plugin would just find that an emulator is already running and skip any waiting and start attempts.

To go even more advanced you could create profiles for each platform level you want to support e.g. 1.6, 2.1 and. 2.2 and specify different avd names in those profiles and then run your build and tests against different platform levels automatically. If you combine this with stopping the emulator and a build server like Hudson you could even do it all automatically. Just saying..

To see the setup of a sample application you can check out the maven android plugin samples fork in my github account and look at the morseflash application. You might also see the zipalign goal in there, which you can find in my fork of the maven android plugin and hopefully in the next release of the plugin.

7 comments » Write a comment

Leave a Reply

Required fields are marked *.