Automating Android test project with Maven

The last post explored how to enable Maven builds in a standard Android Eclipse project. This post will show how to enable the test project to be built with Maven.

It will be a bit simpler to enable Maven builds in the test project since we will not need to do any of the prep work. The only thing required is to create a pom file for the test project:

This pom is similar to the pom in the main project but has two more dependencies. They are both pointing to the main project, one to the apk and one to the jar file. The dependency to the jar file is to enable the compiler to find your java classes from the main project. The dependency to the apk is to enable the android maven plugin to find the apk that it will run the tests against on the device or emulator when executing.

The android maven plugin can now run the tests using instrumentation, just like Eclipse does, as long as an emulator or Android device is connected. It uses the same underlying tools.

In order to execute the test the main project needs to be built and installed into the local Maven repository. This is done with the mvn install command in the main projects root folder. When the main project have been installed the same command, mvn install, will build and execute the tests when executed from within the test projects root.

Next post will explain how to create a project hierarchy that makes it possible to build and test the two projects with one command.

  • Anonymous

    Hi, Thank you for this example. I get an error though, it seems as if maven does not recognize . I get a MavenExecutionException (unrecgnized tag).What can I do to get the build to work? What is missing for me to do?Thanks.

  • Anonymous

    Hi,

    Thank you for this example. I get an error though, it seems as if maven does not recognize . I get a MavenExecutionException (unrecgnized tag).What can I do to get the build to work? What is missing for me to do?

    Thanks.

  • Aaron V.

    I had to change the two 'packaging' elements to be 'type' elements in order for maven 2.2.1 to accept the pom.xml file.

  • Aaron V.

    I had to change the two 'packaging' elements to be 'type' elements in order for maven 2.2.1 to accept the pom.xml file.

  • Tomas Malmsten

    Anonymous, have you found a solution on your problem yet?If not, post your pom file here and I can have a look when I get a minute.Or use the android-maven-plugin Google group to ask questions, you may get a quicker response.

  • Tomas Malmsten

    Anonymous, have you found a solution on your problem yet?

    If not, post your pom file here and I can have a look when I get a minute.

    Or use the android-maven-plugin Google group to ask questions, you may get a quicker response.

  • Nala4ever

    Hi, tried this method and it works fine. However when i try to mock the objects (normal java classes) inside the test classes, i cant do it. No matter what i use, Android mock, easy mock, mockito, power mock etc...
    Any ideas on what could be happening?

  • Anonymous

    To test drive Android development it seems that the solution is to use Robotectric (http://pivotal.github.com/robolectric/index.html). It should run well with Maven. You should also not that this article is pretty old and maven-android have evolved alot since I wrote it. So you should also keep a close look at their website: http://code.google.com/p/maven-android-plugin/

    I hope that helps
    /Tomas