Jacoco can't read .ec file

Viewed 751

To generate code coverage for Unit and Ui tests I've implemented this jacoco.gradlescript

apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.8.5"
}

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
}

project.afterEvaluate {
    android.applicationVariants.all { variant ->
        def variantName = variant.name
        def testTaskName = "test${variantName.capitalize()}UnitTest"
        def uiTestCoverageTaskName = "create${variantName.capitalize()}CoverageReport"

        tasks.create(
                name: "${testTaskName}Coverage",
                type: JacocoReport,
                dependsOn: ["$testTaskName", "$uiTestCoverageTaskName"]) {
            group = "Reporting"
            description = "Generate Jacoco coverage reports for the ${variantName.capitalize()} build."

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

            def excludes = [
                    '**/R.class',
                    '**/R$*.class',
                    '**/BuildConfig.*',
                    '**/Manifest*.*',
                    '**/*Test*.*',
                    'android/**/*.*',
                    '**/*Application*.*',
                    '**/*Dagger*.*',
                    '**/*Hilt*.*',
                    '**/*GeneratedInjectorModuleDeps*.*'

            ]
            def javaClasses = fileTree(dir: variant.javaCompiler.destinationDir, excludes: excludes)
            def kotlinClasses = fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variantName}", excludes: excludes)
            classDirectories.setFrom(files([javaClasses, kotlinClasses]))

            sourceDirectories.setFrom(
                    files([
                            "$project.projectDir/src/main/java",
                            "$project.projectDir/src/${variantName}/java",
                            "$project.projectDir/src/main/kotlin",
                            "$project.projectDir/src/${variantName}/kotlin"
                    ]))

            executionData.setFrom(
                    files([
                            "${project.buildDir}/jacoco/${testTaskName}.exec",
                            "${project.buildDir}/outputs/code_coverage/${variantName}AndroidTest/connected/*coverage.ec"
                    ])
            )

        }
    }
}

I've apply this script in my app.build like this apply from: 'buildscripts/jacoco.gradle'

This task can generate a unit and ui test coverage for a specific flavor. But when I start the gradle task with ./gradlew testDebugUnitTestCoveragethe tests are executing fine but when it comes to collect the UI Test coverage I'm getting this error:

Unable to read execution data file /.../app/build/outputs/code_coverage/debugAndroidTest/connected/*coverage.ec

My project hierarchy looks like this

enter image description here

I'm using Gradle 6.1.1 and Android Gradle Build Tools 4.0.0

1 Answers

In previous versions the name of the file was simply coverage.ec so the wildcard lookup had no problem finding that file. And there are a number of devices that provide a model that does not include spaces, those are also fine.

What you are running into is caused by the name/model of device containing spaces in the name. The wildcard lookup of "*coverage.ec" is not able properly parse the filename with the included spaces.

It is still possible to grab the file, it just takes a bit more work. Swap out:

executionData.setFrom(
    files([
       "${project.buildDir}/jacoco/${testTaskName}.exec",
       "${project.buildDir}/outputs/code_coverage/${variantName}AndroidTest/connected/*coverage.ec"
    ])
)
//Set your UnitTest .exec file
executionData.setFrom(files(["${project.buildDir}/jacoco/${testTaskName}.exec"]))

//You need to make sure this evaluates after the task begins, not at the gradle configuration stage
doFirst {
    //Now look for any files matching *.ec - You can add more details about specific flavor directories if needed.
    def instrumentationTestCoverageDirs = project.fileTree("${project.buildDir}/outputs/code_coverage")
                .matching { include "**/*.ec" }

    //Take this file set and combine it with the UnitTest file set
    def allCodeCoverageFiles = instrumentationTestCoverageDirs.files + executionData.files
    //If you want to log out what files you are including, use this (if it gives warnings on the info lines, you can simply change them to `println`
    project.logger.with {
        info("using following code coverage files for ${taskName}")
        allCodeCoverageFiles.each { coverageFile ->
                info(coverageFile.path)
        }
    }

    executionData.setFrom(allCodeCoverageFiles)
    }
Related