0

我制作了以下 jacoco.gradle 文件

//Reference https://azizbekian.medium.com/setup-jacoco-sonarqube-in-multimodule-multiflavor-kotlin-android-project-d8e7b27aed36
apply plugin: 'jacoco'

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*'] // see related issue https://github.com/gradle/gradle/issues/5184#issuecomment-457865951
}

tasks.withType(Test) { jacoco.includeNoLocationClasses = true }

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",
                ":ordersumary:testDebugUnitTest",
                ":cart:testDebugUnitTest",
                ":login:testDebugUnitTest",
                ":multitender:testDebugUnitTest",
                ":profile:testDebugUnitTest",
                ":storeorders:testDebugUnitTest",
                ":settings:testDebugUnitTest",
                ":search:testDebugUnitTest",
                ":finalconfirmation:testDebugUnitTest",
                ":login:testDebugUnitTest",
//                ":module2:testDebugUnitTest",
//                ":moduleN:testDebugUnitTest",
        ]) {
            group = "Reporting"
            description = "Generate Jacoco coverage reports for the ${variantName.capitalize()} build"

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

            def fileFilter = [
                    '**/*App.*',
                    '**/*Application.*',
                    '**/*Activity.*',
                    '**/*Fragment.*',
                    '**/*JsonAdapter.*', // adapters generated by Moshi
                    '**/di/**',
                    '**/*Dagger.*',
                    '**/*Module.*'
            ]

            classDirectories.setFrom(files([
                    fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variantName}", excludes: fileFilter),
                    fileTree(dir: "$project.rootDir/ordersumary/build/tmp/kotlin-classes/debug", excludes: fileFilter),
                    fileTree(dir: "$project.rootDir/cart/build/tmp/kotlin-classes/debug", excludes: fileFilter),
                    fileTree(dir: "$project.rootDir/common/build/tmp/kotlin-classes/debug", excludes: fileFilter),
                    fileTree(dir: "$project.rootDir/finalconfirmation/build/tmp/kotlin-classes/debug", excludes: fileFilter),
                    fileTree(dir: "$project.rootDir/login/build/tmp/kotlin-classes/debug", excludes: fileFilter),
                    fileTree(dir: "$project.rootDir/multitender/build/tmp/kotlin-classes/debug", excludes: fileFilter),
                    fileTree(dir: "$project.rootDir/profile/build/tmp/kotlin-classes/debug", excludes: fileFilter),
                    fileTree(dir: "$project.rootDir/search/build/tmp/kotlin-classes/debug", excludes: fileFilter),
                    fileTree(dir: "$project.rootDir/settings/build/tmp/kotlin-classes/debug", excludes: fileFilter),
                    fileTree(dir: "$project.rootDir/storeorders/build/tmp/kotlin-classes/debug", excludes: fileFilter),
//                    fileTree(dir: "$project.rootDir/module2/build/tmp/kotlin-classes/debug", excludes: fileFilter),
//                    fileTree(dir: "$project.rootDir/moduleN/build/tmp/kotlin-classes/debug", excludes: fileFilter),
                    fileTree(dir: "$project.buildDir/intermediates/javac/debug/classes", excludes: fileFilter)
            ]))

            def coverageSourceDirs = [
                    "$project.rootDir/app/src/main/java",
                    "$project.projectDir/src/${variantName}/java",
                    "$project.rootDir/ordersumary/src/main/java",
                    "$project.rootDir/cart/src/main/java",
                    "$project.rootDir/common/src/main/java",
                    "$project.rootDir/finalconfirmation/src/main/java",
                    "$project.rootDir/login/src/main/java",
                    "$project.rootDir/multitender/src/main/java",
                    "$project.rootDir/profile/src/main/java",
                    "$project.rootDir/search/src/main/java",
                    "$project.rootDir/storeorders/src/main/java",
                    "$project.rootDir/testing/src/main/java",
//                    "$project.rootDir/module2/src/main/java",
//                    "$project.rootDir/moduleN/src/main/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,
                    "$project.rootDir/ordersumary/build/jacoco/testDebugUnitTest.exec",
                    "$project.rootDir/cart/build/jacoco/testDebugUnitTest.exec",
//                    "$project.rootDir/common/build/jacoco/testDebugUnitTest.exec",
                    "$project.rootDir/finalconfirmation/build/jacoco/testDebugUnitTest.exec",
                    "$project.rootDir/login/build/jacoco/testDebugUnitTest.exec",
                    "$project.rootDir/multitender/build/jacoco/testDebugUnitTest.exec",
                    "$project.rootDir/profile/build/jacoco/testDebugUnitTest.exec",
                    "$project.rootDir/search/build/jacoco/testDebugUnitTest.exec",
                    "$project.rootDir/settings/build/jacoco/testDebugUnitTest.exec",
                    "$project.rootDir/storeorders/build/jacoco/testDebugUnitTest.exec",
//                    "$project.rootDir/module2/build/jacoco/testDebugUnitTest.exec",
//                    "$project.rootDir/moduleN/build/jacoco/testDebugUnitTest.exec"
            ]))
        }
    }
}

我已通过包含以下代码将其包含在我的另一个模块 gradle 文件中,即主模块

apply from: "$project.rootDir/jacoco.gradle"

我也做了 testCoverageEnabled true。

我还在 ./gradlew 任务中看到了代码覆盖率任务

我无法在 home gradle 文件中获取任务名称,它说不存在这样的任务。如何在构建期间运行代码覆盖率,即 /gradlew build。请帮忙。

4

0 回答 0