pandre
pandre

Reputation: 6725

JaCoCo reporting 0% code coverage

I am trying to setup test coverage with jacoco but I've been unsuccessful so far.

In my build.gradle I have added:

apply plugin: 'jacoco'

(...)

buildTypes {
    debug {
        testCoverageEnabled true
    }

(...)

task jacocoTestReport(type: JacocoReport, dependsOn: "test<MyFlavor>DebugUnitTest") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports"

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

    def fileFilter = []
    def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = fileTree(dir: project.projectDir, includes:
            ['**/*.exec' , '**/*.ec'])
}

Then I run the JaCoCo test report with the following code:

./gradlew clean create<MyFlavor>DebugCoverageReport jacocoTestReport

I see that the unit tests are run successfully, but when I open the test report, located in:

<project>/build/reports/jacoco/jacocoTestReport/html/index.html

the report seems to be empty, as coverage is reported as N/A and not even the project packages are displayed.

Moreover, if I try to open the coverage file at

<project>/build/jacoco/test<MyFlavor>DebugUnitTest.exec

using Android Studio, all classes report 0.0% coverage.

I am using gradle 3.0.1

What am I doing wrong? Does this have something to do with the usage of flavors?

Upvotes: 3

Views: 4438

Answers (2)

pandre
pandre

Reputation: 6725

We managed to find a solution to this problem.

It seems that, as we use gradle flavors, we had to add the flavor in the classDirectories line.

Here is our current task, with that change:

task jacocoTestReport(type: JacocoReport, dependsOn: "test<MyFlavor>DebugUnitTest") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports"

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

    def fileFilter = []
    def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/<MyFlavor>/", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = fileTree(dir: project.projectDir, includes:
            ['**/*.exec' , '**/*.ec'])
}

Upvotes: 1

Christopher
Christopher

Reputation: 10259

I was also struggling with UnitTests and JaCoCo. I solved my problem by using a different plugin.

In your root build.gradle add:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.10.0'
    }
}

and apply the plugin in your module build.gradle:

apply plugin: 'com.vanniktech.android.junit.jacoco'

After a sync you should have new gradle tasks:

jacocoTestReport<<BuildVariant>>

Upvotes: 1

Related Questions