在build.gradle文件中配置Jacoco,可以使用以下代码来避免再次插装类:
android {
buildTypes {
debug {
testCoverageEnabled true
}
}
}
jacoco {
toolVersion = "0.8.4"
reportsDir = file("$rootDir/reports/jacoco")
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) {
reports {
xml.required = false
html.enabled = true
}
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"
def mainJava = fileTree(dir: mainSrc, includes: ['**/*.java'])
def jacocoTree = fileTree(dir: "${buildDir}/jacoco/testDebugUnitTest.exec", excludes: fileFilter)
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = files([jacocoTree])
}
这段代码将在测试时启用Jacoco报告,并在生成报告时排除R类,BuildConfig和测试类。注意,使用这段代码,与Jacoco一起使用的插件需要是0.8.4或更高版本的插件。