Home » Android » androidJacocoAnt configuration no longer found with Android Gradle plugin 3.1.0

androidJacocoAnt configuration no longer found with Android Gradle plugin 3.1.0

Posted by: admin June 15, 2020 Leave a comment

Questions:

After updating to Android Gradle plugin 3.1.0 (from 3.0.1) my JaCoCo unit test coverage configuration started producing errors in Gradle config phase:

> Configuration with name 'androidJacocoAnt' not found.

Project-level build.gradle:

dependencies {
    classpath 'com.android.tools.build:gradle:3.1.0'
    classpath "org.jacoco:org.jacoco.core:0.8.1"
}

Module-level build.gradle:

apply plugin: 'jacoco'

...

android {
    buildTypes {
        debug {
            testCoverageEnabled true
        }
    }

    ...
}

task jacocoTestReport(type: JacocoReport) {
    dependsOn 'createDebugCoverageReport'
    dependsOn 'testDebugUnitTest'

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

    jacocoClasspath = configurations['androidJacocoAnt']

    def fileFilter = [
            '**/R.class',
            '**/R$*.class',
            '**/BuildConfig.*',
            '**/Manifest*.*',
            '**/*Test*.*',
            'android/**/*.*'
    ]

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

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = fileTree(dir: "$buildDir", includes: [
            "jacoco/testDebugUnitTest.exec",
            "outputs/code-coverage/connected/*coverage.ec"
    ])
}
How to&Answers:

Diffing the output of gradle dependencies with different plugin versions, it seems that

jacocoClasspath = configurations['androidJacocoAnt']

needs to change to

jacocoClasspath = configurations['jacocoAnt']

Answer:

Do you need JaCoCo 8.1 in latest Android Gradle Plugin 3.1 or higher?

issue is in gradle plugin, in it version of the JaCoCo is hardcoded to 0.7.9

Workaround (step 1 from 2): Add into root build.gradle

buildscript {
    ext {
        jacocoVersion = project.JACOCO_VERSION
    }
    dependencies {
        /* To confirm JaCoCo version run: $ ./gradlew buildEnvironment */
        //region classpath "org.jacoco:org.jacoco.core:${jacocoVersion}"
        /* Resolves issue of incorrect version use in one of jacoco/android plugin inner tasks */
        classpath "org.jacoco:org.jacoco.core:${jacocoVersion}"
        classpath "org.jacoco:org.jacoco.report:${jacocoVersion}"
        classpath "org.jacoco:org.jacoco.agent:${jacocoVersion}"
        //endregion
    }
}

Workaround (step 2 from 2): Add into root build.gradle

/* Force Jacoco Agent version upgrade */
subprojects {
    configurations.all {
        resolutionStrategy {
            eachDependency { details ->
                if ('org.jacoco' == details.requested.group) {
                    details.useVersion "${jacocoVersion}"
                }
            }
        }
    }
}

Enjoy