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

  1. Great. Together with Hudson Axis, one can easily run a batch of tests targeted at different emulator configurations (resolutions, o.s. versions, etc.).

  2. Although I have a very hard time getting all the Maven+Android stuff to work, I must simply say it again: THANK YOU! It’s always good to see people do pioneering work in a certain area. And testing on Android is certainly uncharted territory.

  3. I am sorry you are having such a hard time. It is currently transitioning from the sdk deployer artifacts to the maven central ones so things are a bit in turmoil. If you use the sdk deployer the instructions in the android chapter of Maven: The complete reference are correct.

  4. Hi,

    Thanks for the write up but I’m having issues when running the apidemos-15 build for the 2.6.0 plugin. The apidemos-15-app runs fine then when it runs the instrumentation tests module it runs through and spits out a bunch of activity information then hits the waiting for device line (see last two lines below):

    [INFO] /Users/jay/workspace/3rdparty/android/1.5/mac/tools/adb [uninstall,]
    [INFO] – waiting for device –

    It doesn’t continue on after this and just hangs. I have to Ctrl-C it to get out. What am I doing wrong as everything works up until this point.


  5. very nice work, finally i can use nb to build apps with all the features, with use of profiles and hudson the daily work comes lighter thanks a lot,

  6. Hi,

    I am using your method to start the emulator. However, the emulator is in the locked screen after starting which will cause the test not running. Is there a way to unlock the screen after starting the emulator?


Leave a Reply

Required fields are marked *.