未生成 jacocoTestReport 任务

jacocoTestReport task not being generated

Android Studio 3.1 Canary 8
Build #AI-173.4529993, built on January 6, 2018
JRE: 1.8.0_152-release-1024-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.14.14-300.fc27.x86_64

我正在尝试使用 jacoco 生成代码覆盖率。但是,当我 运行 命令 ./gradlew tasks 时,我没有看到任何名为 jacocoTestReport.

的任务

当我尝试 运行 任务 ./gradlew jacocoTestReport:

时出现以下错误

Task 'jacocoTestReport' not found in root project 'EnumSample'

这是我的 build.gradlew 文件:

apply plugin: 'com.android.application'
apply plugin: 'jacoco'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "me.androidbox.enumsample"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            testCoverageEnabled true
        }
    }
}

jacoco {
    toolVersion "0.8.0"
}

task jacocoTestReport(type: JacocoReport) {
    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")

    subprojects.each {
        sourceSets it.sourceSets.main
    }

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

我已尝试清理并重建项目。但是,报告任务不存在。

非常感谢您的任何建议。

您正在搜索要执行的错误任务。通过执行 ./gradlew tasks,您将能够找到 createFlavorCoverageReport 任务:

使用您在问题中提到的设置执行 ./gradlew createDevDebugCoverageReport 后,我能够在 /app/build/reports/dev/debug 目录中找到生成的报告。

有两件事:

  • 您需要 enable the code coverage 支持您将用于测试的构建类型。您的 build.gradle 应包含以下内容(您已经包含):

    android { 
        ... 
        buildTypes { 
            debug { 
                testCoverageEnabled = true 
            } 
            ... 
        } 
        ... 
    }
    
    • 要生成报告,运行 gradle testBlueDebugUnitTestCoverage 您将在 “build/reports/jacoco/testBlueDebugUnitTestCoverage/”
    • 中看到它们
    • 这是一个gitHub example

  • 使用生成 JaCoCo 报告的Gradle plugin

    设置如下:

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.11.0'
        }
    }
    
    apply plugin: 'com.vanniktech.android.junit.jacoco'
    

报告问题的另一个解决方案here

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") { 
    group = "Reporting" 
    description = "Generate Jacoco coverage reports after running tests." 
    reports { 
        xml.enabled = false 
        html.enabled = true 
    } 
    classDirectories = fileTree( 
            dir: './build/classes/debug', 
            excludes: ['**/R.class', 
                       '**/R$*.class', 
                       '**/*$InjectAdapter.class', 
                       '**/*$ModuleAdapter.class', 
                       '**/*$ViewInjector*.class' 
            ]) 
    sourceDirectories = files(coverageSourceDirs) 
    executionData = files('build/jacoco/testDebug.exec') 

    renamedFilesMap = [:] 

    // Hacky fix for issue: https://code.google.com/p/android/issues/detail?id=69174. 
    // Rename files with '$$' before generating report, and then rename back after 
    doFirst { 
        new File('build/classes/debug').eachFileRecurse { file -> 
            if (file.name.contains('$$')) { 
                oldPath = file.path 
                newPath = oldPath.replace('$$', '$') 
                file.renameTo(newPath) 
                renamedFilesMap[newPath] = oldPath 
            } 
        } 
    } 
    doLast { 
        renamedFilesMap.each() { 
            newPath, oldPath -> 
                new File(newPath).renameTo(oldPath) 
        } 
    } 

}

在使用jacoco报告时我们需要注意以下几点:

在 app/build 中启用了测试覆盖率。gradle

android {
    ...
    buildTypes {
        debug {
            testCoverageEnabled true
        }
        ...
    }
}

为 jacoco 报告创建任务

apply plugin: 'jacoco'

task jacocoTestReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {

    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 = files("${buildDir}/jacoco/testDebugUnitTest.exec")
}

Gradle jacoco 报告命令

./gradlew clean jacocoTestReport

在此处查找 jacoco 报告

成功执行 jacocoTestReport 后生成的 jacoco 报告路径。

app/build/reports/coverage/debug/index.html

此外,我创建了一个 android jacoco 相关示例存储库,您可以在其中查看。

https://github.com/jiteshmohite/JacocoAndroidSample

此外,请确保您在应用程序目录中执行 运行 Gradle 命令。

尝试参考上述示例存储库。我以零复杂性创建了它,因此每个人都可以去使用它。