Using Maven to automate build and test of Android in one command

The last post explored how to automate a Android test project that is associated with an Android application. It also introduces some unneeded complexity in how each project has to be built individually for the tests to run. This is easy to do away with by using Mavens built in project hierarchy.

To achieve this a new directory needs to be created. In the new directory the two android projects (the application and the test projects) should be placed. Ensure that the names of the directories matches that of the artifact name specified in the pom for each project.

Now a pom.xml needs to be created that holds information which is similar for both project and also reference the two projects. Mine look like this:

It is important to note what the names in the modules section are. They need to correspond with the directory names of the two modules, and the directory names should in turn match the artifact id's of each of the modules.

Next we will need to change the pom files of the two modules, the application and test pom files. The following needs to change:

  • The build section can be removed. It will be inherited from here.
  • The child projects need to know it's parent. This is done by adding the following to the top of each pom:

  • In the dependencyManagement section in the parent pom the version number and scope for the android jar file is specified. That makes this information is redundant in the child poms. So removing it will ensure clean build files.

Once this is done you can go to the directory where your parent project is and run the command:

This will trigger the build and test of your Android project.

Having done this it is very simple to add your Android project to your favorite CI server. What you need to keep in mind is that in order for the tests to work you need to have either a connected device or a running emulator.

  • Quentin

    Hi,Thanks for this three last posts, they really help me. I have still some problems to solve. I can launch main and tests projects with one command on my machine (iMac), but it does not work on our hudson server (Windows). Error is:INSTRUMENTATION_STATUS: Error=Unable to find instrumentation target package: com.project.android.applicationINSTRUMENTATION_STATUS: id=ActivityManagerServiceINSTRUMENTATION_FAILED: com.project.android.application.tests/android.test.InstrumentationTestRunnerRegards,

  • Quentin

    Hi,

    Thanks for this three last posts, they really help me. I have still some problems to solve. I can launch main and tests projects with one command on my machine (iMac), but it does not work on our hudson server (Windows). Error is:

    INSTRUMENTATION_STATUS: Error=Unable to find instrumentation target package: com.project.android.application

    INSTRUMENTATION_STATUS: id=ActivityManagerService

    INSTRUMENTATION_FAILED: com.project.android.application.tests/android.test.InstrumentationTestRunner

    Regards,

  • Tomas Malmsten

    I'm afraid I haven't gotten as far as getting this up and running on a Hudson server yet. But go to the Android Maven home page and have a look around there if anyone has had a similar problem. Or if not, post a question in the group.If you do find a solution then please share with me, or at the projects home page.Good that you have found this useful.RegardsTomas

  • Tomas Malmsten

    I'm afraid I haven't gotten as far as getting this up and running on a Hudson server yet. But go to the Android Maven home page and have a look around there if anyone has had a similar problem. Or if not, post a question in the group.

    If you do find a solution then please share with me, or at the projects home page.

    Good that you have found this useful.

    Regards
    Tomas