更新到Android Gradle插件3.1.0(从3.0.1)后,我的JaCoCo单元测试覆盖率配置开始在Gradle配置阶段产生错误:
> Configuration with name 'androidJacocoAnt' not found.
项目级build.gradle:
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
classpath "org.jacoco:org.jacoco.core:0.8.1"
}
模块级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"
])
}
用不同的插件版本来区分gradle dependencies
的输出,似乎是这样
jacocoClasspath = configurations['androidJacocoAnt']
需要改变
jacocoClasspath = configurations['jacocoAnt']
在最新的Android Gradle Plugin 3.1或更高版本中你需要JaCoCo 8.1吗?
问题是在gradle插件中,其中JaCoCo的版本硬编码为0.7.9
解决方法(从2开始的步骤1):添加到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
}
}
解决方法(从2开始的步骤2):添加到root build.gradle中
/* Force Jacoco Agent version upgrade */
subprojects {
configurations.all {
resolutionStrategy {
eachDependency { details ->
if ('org.jacoco' == details.requested.group) {
details.useVersion "${jacocoVersion}"
}
}
}
}
}
请享用