Serj Lotutovici Serj Lotutovici - 1 year ago 259
Android Question

Jacoco and Unit Tests Code Coverage with android-gradle-plugin >= 1.1

I recently started integrating

1.1.0 in one of my projects. The project uses
robolectric 2.4
to run unit tests.

It's a multi module project with very complex dependencies (Some modules depend on other modules). Something like that:

--> application-module (dependsOn: module1, module2, module-core)
--> module1 (dependsOn: module-core)
--> module2 (dependsOn: module-core)
--> module-core (dependsOn: module3, module4)
--> module3 (library dependencies)
--> module4 (library dependencies)

For a more cleared picture please see jacoco-example project.

I tried to integrate JaCoCo to generate reports for the unit tests, but it seems to me that it runs only
which are basically instrumentation tests.

After some google'ing I've come across a few projects on GitHub and other articles, but they mainly are focused on previous versions of the
or are using other third party plugins like
for example here.

May be I've lost my ability to google. But can somebody point me in a direction where I can find some documentations regarding the new stuff in android gradle plugin and how to run the jacoco task only for unit tests?


Adopted the script from nenick's example:

apply plugin: "jacoco"

configurations {

task jacocoReport(dependsOn: 'testDebug') << {
ant {
classname: 'org.jacoco.ant.ReportTask',
classpath: configurations.jacocoReport.asPath)

mkdir dir: "${buildDir}/test-coverage-report"
mkdir dir: "${buildDir}/reports/jacoco/test/"

jacocoreport {
executiondata = files("${buildDir}/jacoco/testDebug.exec")

structure(name: "${}") {
classfiles {
fileset (dir: "${buildDir}/intermediates/classes/debug") {
//exclude(name: '**/*_*.class')
exclude(name: '**/R.class')
exclude(name: '**/R$*.class')
exclude(name: '**/BuildConfig.class')

sourcefiles {
fileset dir: "src/main/java"
fileset dir: "${buildDir}/generated/source/buildConfig/debug"
fileset dir: "${buildDir}/generated/source/r/debug"

xml destfile: "${buildDir}/reports/jacoco/test/jacocoTestReport.xml"
html destdir: "${buildDir}/test-coverage-report/"

dependencies {
jacocoReport 'org.jacoco:org.jacoco.ant:'

After that the
./gradlew jacocoReport
executes and generates the report, but it shows 0 (zero) test coverage, which is impossible because at least half of all classes are tested.


Tried out this example. Adding the next task to one of my gradle build files:

task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports"

classDirectories = fileTree(
dir: "${buildDir}/intermediates/classes/debug",
excludes: ['**/R.class',

sourceDirectories = files("${buildDir.parent}/src/main/java")
additionalSourceDirs = files([
executionData = files("${buildDir}/jacoco/testDebug.exec")

reports {
xml.enabled = true
html.enabled = true

Same issue, the reports are generated, but the code coverage is still zero.


It seams that the task from UPDATE_2 worked but only for the module with
apply plugin: ''
(The reports a generated correctly). But for modules that are android libraries (
apply plugin: ''
) the reports show zero coverage, although the modules contain more tests then the application module.


Created a simple example project that demonstrates my issue. Currently if you run
./gradlew jacocoReport
the report is generated, but no test coverage is displayed for the module projects. See this link

Short note: When the tests were AndroidUnitTests (whiteout JUnit 4 and Robolectric) JaCoCo reports showed coverage for all the modules.

Any ideas?

Answer Source

After the hassle I decided to create an open source Gradle plugin for that.

Root build.gradle

buildscript {
    repositories {
        mavenCentral() // optional if you have this one already
    dependencies {
        classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.5.0'

apply plugin: ''

Then simply execute

./gradlew jacocoTestReportDebug

It'll run the JUnit tests in Debug Mode and then give you the Jacoco output in xml and html form in the corresponding build directory.

It also supports flavors. Having 2 flavors red and blue those tasks would be created

  • jacocoTestReportRedDebug
  • jacocoTestReportBlueDebug
  • jacocoTestReportRedRelease
  • jacocoTestReportBlueRelease