Jacoco Code Coverage in android studio

I see that you already got it working, however, there's a simpler method for getting Unit Test execution data. I recently was looking into this as well, I actually made a full write up earlier today.

In my situation, I didn't want to create an additional Gradle task as I wanted the report to be generated as a part of the existing workflow. I also didn't want to explicitly add the Jacoco plugin, as Google already dups the Jacoco Ant tasks for the coverage reports for Instrumentation Tests.

In addition to setting the properties android.jacoco.version and buildTypes.debug.testCoverageEnabled, I added the following to the testDebug JVM arguments to generate execution data:

project.afterEvaluate {
  def append = "append=true"
  def destFile = "destfile=$buildDir/outputs/code-coverage/connected/coverage.ec"
  testDebug.jvmArgs "-javaagent:$buildDir/intermediates/jacoco/jacocoagent.jar=$append,$destFile"

  createDebugCoverageReport.dependsOn testDebug
}

This appends the Unit Test execution data to the coverage file generated by connectedAndroidTest, so your report reflects both Instrumentation Tests and Unit Tests, rather than each variant individually.

Note that connectedAndroidTest overwrites the coverage file, take this into account when creating your report. If the task testDebug doesn't have any changes, and you run createDebugCoverageReport, it will only reflect your Instrumentation Test coverage. So, make a change to your Unit Tests. The Linux command touch may be useful here, although I haven't tried yet.


Today I did completely removed android studio, android sdk, gradle. Then reinstall everything. After that, I just added inside the app build.gradle.

debug {
    testCoverageEnabled true
}

Then I run ./gradlew connectedChec. Everything is working perfectly. Android studio default Jacoco working fine for me. I think it is also possible to create a jacocoTestReport Task and then create code coverage.I don't know why gradle and android studio was not working previously.


Gradle already has built-in support for generating test coverage reports and we don't need to create any additional configurations or add any plugins to generate test coverage report. Basically, the only thing we need to do is to set testCoverageEnabled parameter to true in build.gradle file as follows:

android {
   buildTypes {
      debug {
         testCoverageEnabled = true
      }
   }
}

Next, we can execute the following Gradle task from CLI:

./gradlew createDebugCoverageReport

On Windows, we can execute it like this:

gradlew.bat createDebugCoverageReport

Task will analyze code of our project in /src/main/java/ directory and unit tests placed in /src/androidTest/java/ directory. After executing this task, we can find test coverage report in the following directory of the module:

/build/outputs/reports/coverage/debug/

When we open index.html file, we can see a visual report from test coverage, which can be viewed in a web browser.

It looks as on the image below.

enter image description here

I've written an article about test coverage report in Android application and published it on my blog some time ago. If you are interested in that topic, you can read it at:

  • https://web.archive.org/web/20171210224809/http://blog.wittchen.biz.pl/test-coverage-report-for-android-application (Old blog, with user comments)
  • http://wittchen.io/test-coverage-in-android-applications/ (New blog, without user comments)

Update (Unit test coverage for Robolectric tests)

If you would like to create test coverage report from unit tests written with Robolectric and generate test coverage report with Jacoco, Travis CI and Codecov.io, please take a look at the new article on my blog:

  • https://web.archive.org/web/20171013015925/http://blog.wittchen.biz.pl/test-coverage-report-with-travis-ci-for-robolectric-tests-on-android/ (Old blog)
  • http://wittchen.io/unit-test-coverage-report-with-travis-ci-for-robolectric-on-android/ (New blog, without user comments)