3

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?

1
  • Please show the gradle build output (limited to Jacoco details). Commented Jan 23, 2018 at 15:30

2 Answers 2

1

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>>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. I tried that plugin, but it seems like it is not compatible with gradle 3.0.1 :(
Hmm, I don't think so. We have the same gradle android plugin version... But we used version 0.10.0 of this plugin in our code.
Perfect!. This solution actually worked like a charm :)
1

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'])
}

1 Comment

from where one is having this file path "test<MyFlavor>DebugUnitTest")..how can i setup this in my gradle folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.