Jacoco Code Coverage on Android

Alright, so you want to determine how well your test coverage is on Android? Good news, Android Tools has test coverage support since 0.10.

It's fairly simple to get working if you don't fight it. First, add testCoverageEnabled = true to your buildTypes.debug closure in your build.gradle. Add jacoco { version = "" } to the android closure. Now, run the createDebugCoverageReport task. You'll find your test coverage report in the directory $buildDir/outputs/reports/coverage/debug. This coverage report is limited to Instrumentation Tests.

If you're looking to integrate your Unit Test coverage, then you'll need to make a few adjustments to the createDebugCoverageReport task. Bear in mind that this is currently a known bug, so look into this further, it may become a little easier in the future.

Currently, Jacoco only creates execution data for the task connectedAndroidTest, which is an Instrumentation Test task. You'll need to adjust the Unit Test task testDebug to generate execution data for your other tests. The Android build-system currently (internally) duplicates the Jacoco Ant tasks, making a limited JacocoReportTask available through Gradle. If you want more leeway, then consider doing the same. However, personally, I just wanted execution data generated for my Unit Tests. Adding the following to my build.gradle:

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 is sufficient to append the Unit Test execution data to the coverage file used in the coverage report. Now, the order in which you run your Gradle tasks matters. The createDebugCoverageReport task doesn't append data to the coverage file, it writes from the beginning. Additionally, if you haven't made any changes to your Unit Test since you last ran testDebug, your Unit Test execution data won't get appended. For the time being, you can run the tasks in the following order: createDebugCoverageReport (create Instrumentation Test execution data, generate report), make a change to your Unit Tests, createDebugCoverageReport (append Unit Test execution data, and regenerate report). If you run into issues, then play around with your "Test Artifact" settings under the "Build Variants" tab on the left-hand side of Android Studio.

Thanks to this Stack Overflow question for helping me get the report created at all.