如何在 Jacoco 报告生成中制作 "sourceDirectories" public?

How to make "sourceDirectories" public in Jacoco report generation?

我正在关注 this post 为我的项目设置 Jacoco 代码覆盖率。

但是我在编写生成报告的任务时遇到错误。

Gradle 任务

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'
    ])
}

我得到的错误是:

Cannot set the value of read-only property 'sourceDirectories' for task ':app:jacocoTestReport' of type org.gradle.testing.jacoco.tasks.JacocoReport.

现在我相信 jacoco 以下字段是私有的。

我的依赖:classpath "org.jacoco:org.jacoco.core:0.8.4"

此外,我尝试更改版本但没有帮助。

找到答案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 .