为什么启用覆盖时android Instrumentation测试报告会失败?

时间:2016-11-22 16:38:26

标签: android jacoco test-coverage

我正在尝试生成jacoco报告,但是当我将testCoverageEnabled属性设置为true时,我一直收到错误。

java.lang.NoSuchMethodError: No static method $jacocoInit()[Z in class L...

删除 testCoverageEnabled 属性我可以运行测试,但无法生成报告。

我正在使用标准的Jacoco配置。单独测试和集成测试在独立运行时正常运行。

此问题的所有在线参考似乎都是项目中的特定配置问题。任何帮助表示赞赏。

更新

apply plugin: 'jacoco'

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: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
    def mainSrc = "${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"
    ])
}

1 个答案:

答案 0 :(得分:0)

我的工作配置。可能它可以帮助你。

  apply plugin: 'jacoco'

        def coverageSourceDirs = [
                '../app/src/main/java'
        ]

        jacoco {
            toolVersion = "0.7.6.+"
        }

        task jacocoTestReport(type: JacocoReport, dependsOn: "testDebugUnitTest") {
            group = "Reporting"
            description = "Generate Jacoco coverage reports"

            classDirectories = fileTree(
                    dir: '../app/build/intermediates/classes',
                    excludes: ['**/R.class',
                               '**/R$*.class',
                               '**/*$ViewInjector*.*',
                               '**/BuildConfig.*',
                               '**/Manifest*.*']
            )


            additionalSourceDirs = files(coverageSourceDirs)
            sourceDirectories = files(coverageSourceDirs)
            executionData = files('../app/build/jacoco/testDebugUnitTest.exec')

            reports {
                xml.enabled = true
                html.enabled = true
            }
        }

并在buildTypes中添加此

testCoverageEnabled = true