Home » Android » How to get code coverage in Android using Maven (android-maven-plugin)

How to get code coverage in Android using Maven (android-maven-plugin)

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have an Android Maven project (let’s call it parent_project) that contains various submodules: my_library_project, app_using_my_library_project, test_project and extra_lib.

So, the structure would be like this:

parent_project
   * my_library_project (Android Library Project)
   * app_using_my_library_project (Demo app that uses the Android Library Project)
   * test_project (Project containing the tests instrumented against app_using_my_library_project)
   * extra_lib

What I would like is to generate test coverage for my Android project using Maven (and not Ant, I am already able to generate code coverage reports using Ant, following these instructions: https://wiki.jenkins-ci.org/display/JENKINS/Building+an+Android+app+and+test+project).

I have no strong preference for the code coverage tool used but I would prefer EMMA, since seems the most common in the Android development world.

I am using android-maven-plugin (http://code.google.com/p/maven-android-plugin/) in its 3.0.0-alpha-12 version and I have already tried to put in the configuration of my parent’s pom.xml the next:

<test>
  <coverage>true</coverage>
  <createreport>true</createreport>
</test>

But that does not produce the desired code coverage report.

So:

  • Is there any difference between the pom configuration for getting code coverage for a standard Java project and an Android project?
  • Do you know any example Android project using Maven that has code coverage?
  • Any hints on how to do this?
How to&Answers:

If you’re going to stick with maven, and want a plugin for maven that will do the code-coverage job, I think Cobertura is a better choice, as Emma stable last build is from 2005.

Although in “Android Application Testing Guide” (a recent book from June this year) they talk about Emma and demonstrate how to use it for testing, I think people stick to it, because it’s needed to build Android from source (and if Google use it for their own OS development, it should be the best, right?).

If you’re not fanatically bound to Maven, I strongly recommend to try Robotium.
Robotium has full support for Activities, Dialogs, Toasts, Menus, and Context Menus.
It also supports code coverage (Ant based though, for now) and some people recognize it as one of the leading testing platforms for Android.

Edit:

According to the Cobertura site, it supports code coverage in Maven 1 and Maven 2 environments. Although, you can find examples with Maven 3 also.
A problem exists between pom configurations of Maven 2 and Maven 3. It seems for the reporting to work you have to basically move your old reporting plugins into the configuration section of the new maven-site-plugin. (See the article for details).

Another option is to try and use Sonar with Maven. Sonar has cobertura embedded (also options to embedd EMMA) and some people state that they had successfully reported code coverage, despite they had problems using the “stand-alone” cobertura plugin.

Answer:

I could generate code coverage reported using emma maven plugin and display reports in sonar for an android application project. Just follow the configuration in https://code.google.com/p/maven-android-plugin/wiki/EmmaMaven.
But for library project I get a 0% coverage. It doesn’t generate the metadata files. However as soon I change the library project to package as an apk, it works like a charm. It runs the tests in a emulator(configured in jenkins) and shows the coverage reports.
If you are looking out specifically for library project, this wont help. I will post if I find some solution which deals with library projects.