问题
I was following this post to setup Jacoco code coverage for my project.
But I am facing a error while writing the task to generate the report.
Gradle Task
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
def debugTree = fileTree(dir: "$project.buildDir/intermediates/javac/debug", excludes: fileFilter)
def mainSrc = "$project.projectDir/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = fileTree(dir: project.buildDir, includes: [
'jacoco/testDebugUnitTest.exec', 'outputs/code_coverage/debugAndroidTest/connected/**/*.ec'
])
}
The error I am getting is :
Cannot set the value of read-only property 'sourceDirectories' for task ':app:jacocoTestReport' of type org.gradle.testing.jacoco.tasks.JacocoReport.
Below field's are private in the jacoco
now I believe.
- sourceDirectories
- classDirectories
- executionData
My dependency : classpath "org.jacoco:org.jacoco.core:0.8.4"
Also, I tried to change the version but no help.
回答1:
Found the Answer here
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
def debugTree = fileTree(dir: "$project.buildDir/intermediates/javac/debug", excludes: fileFilter)
def mainSrc = "$project.projectDir/src/main/java"
sourceDirectories.from files([mainSrc])
classDirectories.from files([debugTree])
executionData.from fileTree(dir: project.buildDir, includes: [
'jacoco/testDebugUnitTest.exec', 'outputs/code_coverage/debugAndroidTest/connected/**/*.ec'
])
}
(sourceDirectories =) in not allowed in latest gradle version instead (sourceDirectories.from) should be used .
来源:https://stackoverflow.com/questions/62740456/how-to-make-sourcedirectories-public-in-jacoco-report-generation