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"
])
}
A Gradle project describes its build in a file called build. gradle located in the root folder of the project.
7.1.2 (February 2022) Android Gradle Plugin 7.1.
In Android Studio, go to File > Project Structure. Then select the "project" tab on the left. Your Gradle version will be displayed here.
The Android Gradle plugin (AGP) is the official build system for Android applications. It includes support for compiling many different types of sources and linking them together into an application that you can run on a physical Android device or an emulator.
Diffing the output of gradle dependencies
with different plugin versions, it seems that
jacocoClasspath = configurations['androidJacocoAnt']
needs to change to
jacocoClasspath = configurations['jacocoAnt']
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With