3

摇篮版本:6.1.1

Android Gradle 插件:4.0.0

在尝试运行我的 jacoco 覆盖时,它似乎没有考虑到版本。当我查看 HTML 报告时,它显示“使用 JaCoCo 0.7.9.201702052155 创建”,尽管我toolVersion = "0.8.5"在我的设置中。该报告也没有我期望在 0.8.3 中与 Kotlin 相关的修复,作为辅助确认这不起作用。

apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.8.5"
    reportsDir = file("$buildDir/reports")
}

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']
}

project.afterEvaluate {
    (android.hasProperty('applicationVariants')
            ? android.'applicationVariants'
            : android.'libraryVariants').all { variant ->
        def variantName = variant.name
        def unitTestTask = "test${variantName.capitalize()}UnitTest"
        def uiTestCoverageTask = "create${variantName.capitalize()}CoverageReport"

        tasks.create(name: "${unitTestTask}Coverage", type: JacocoReport, dependsOn: [
                "$unitTestTask",
                "$uiTestCoverageTask"
        ]) {
            group = "Reporting"
            description = "Generate Jacoco coverage reports for the ${variantName.capitalize()} build"

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

            def fileFilter = [
                    '**/R.class',
                    '**/R$*.class',
                    '**/BuildConfig.*',
                    '**/Manifest*.*',
                    '**/*Test*.*',
                    '**/com/example/databinding/*',
                    '**/com/example/generated/callback/*',
                    '**/android/databinding/*',
                    '**/androidx/databinding/*',
                    '**/di/module/*',
                    '**/*MapperImpl*.*',
                    '**/*$ViewInjector*.*',
                    '**/*$ViewBinder*.*',
                    '**/BuildConfig.*',
                    '**/*Component*.*',
                    '**/*BR*.*',
                    '**/Manifest*.*',
                    '**/*$Lambda$*.*',
                    '**/*Companion*.*',
                    '**/*Module.*',
                    '**/*Dagger*.*',
                    '**/*MembersInjector*.*',
                    '**/*_Factory*.*',
                    '**/*_Provide*Factory*.*',
                    '**/*Extensions*.*',
                    '**/*$Result.*', /* filtering `sealed` and `data` classes */
                    '**/*$Result$*.*'/* filtering `sealed` and `data` classes */
            ]

            classDirectories.setFrom(files([
                    fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variantName}", excludes: fileFilter)
            ]))

            def coverageSourceDirs = [
                    "$project.rootDir/app/src/main/java",
                    "$project.projectDir/src/${variantName}/java"
            ]
            additionalSourceDirs.setFrom(files(coverageSourceDirs))
            sourceDirectories.setFrom(files(coverageSourceDirs))

            def uiTestsData = fileTree(dir: "${buildDir}/outputs/code_coverage/${variantName}AndroidTest/connected/", includes: ["**/*.ec"])

            executionData(files([
                    "$project.buildDir/jacoco/${unitTestTask}.exec",
                    uiTestsData
            ]))
        }
    }
}
4

3 回答 3

4

正如@Bilgehan KALKAN 所说,从Gradle 7 开始,这似乎已经解决。

android {
    //Other configurations...
    testCoverage.jacocoVersion = "0.8.7"
}

对于较低的 gradle 版本,请尝试将其添加到您的项目 Gradle 文件中。

subprojects {
    configurations.all {
        resolutionStrategy {
            eachDependency { details ->
                if ('org.jacoco' == details.requested.group) {
                    details.useVersion "0.8.6"
                }
            }
        }
    }
}
于 2021-01-05T13:25:39.377 回答
2

您不需要覆盖@LethalMaus 回答中提到的版本。您只需要在 as 下指定 jacoco 版本androidtestCoverage.jacocoVersion@ jacoco.versionAlexy Nikitin 的答案的方法已被弃用并替换为testCoverage.jacocoVersion

如果不指定任何版本,它将DEFAULT_VERSION在 JacocoOptions 类下使用。这适用"0.8.3"于 Android Gradle 插件版本 7.0.2。

于 2021-10-04T10:26:02.790 回答
0

您也应该在android gradle 插件中声明 jacoco 版本:

android {
    jacoco {
        version "<your jacoco version>"
    }
}
于 2021-10-03T16:01:56.153 回答