android - 找不到 Jacoco Android createDebugCoverageReport

标签 android gradle jacoco

我想在 Android 应用程序中运行我的测试并创建覆盖率报告,所以我将 Jacoco 配置添加到我的 build.gradle 文件中,但它不起作用。

apply plugin: 'com.android.application'

android {

    compileSdkVersion 22
    buildToolsVersion '22.0.1'

    defaultConfig {
        applicationId "mm"
        minSdkVersion 12
        targetSdkVersion 18
    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

dependencies {
    compile 'com.google.code.gson:gson:2.1'
    compile files('libs/android-async-http-1.4.4.jar')
    compile files('libs/freemarker.jar')
    compile files('libs/greendao-1.3.1.jar')
    compile files('libs/raygun4android-1.1.0.jar')
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:recyclerview-v7:22.2.0'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    compile 'junit:junit:4.11'

    androidTestCompile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }

}

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.1'
        classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
    }
}

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

jacoco {
    version "0.7.1.201405082137"
}


jacoco {
    toolVersion "0.7.1.201405082137"
}

def coverageSourceDirs = [
        'src/main/java',
]

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
    }
    classDirectories = fileTree(
            dir: './build/intermediates/classes/debug',
            excludes: ['**/R*.class',
                       '**/*$InjectAdapter.class',
                       '**/*$ModuleAdapter.class',
                       '**/*$ViewInjector*.class'
            ])
    sourceDirectories = files(coverageSourceDirs)
    executionData = files("$buildDir/jacoco/testDebug.exec")
    doFirst {
        new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    }
}

我知道,gradle 版本 1.3.0 和 1.3.1 存在问题,它应该可以正常工作,但是对于 1.3.1,我得到 Task 'createDebugCoverageReport' not found in root project

最佳答案

您必须启用 testCoverageEnabled :

buildTypes {
    debug{
        debuggable true
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        isTestCoverageEnabled true
    }
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        isTestCoverageEnabled true
    }
}

关于android - 找不到 Jacoco Android createDebugCoverageReport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33037537/

相关文章:

android - 创建firebase时错误构建gradle

maven - gradle 不是像 Maven 有 m2 那样有自己的仓库吗?

java - 为什么没有检测到 JAVA 版本? Android Gradle 插件需要 Java 11 才能运行。您当前使用的是 Java 1.8

android - Gradle 在 Ubuntu 13.10 上调用 "completing successfully with 1 error"

groovy - 使用 GroovyShell 从 Gradle 运行 Groovy 脚本 : Exception in thread "main" java. lang.NoClassDefFoundError: org/apache/commons/cli/ParseException

android - Gradle 找不到 'testDebug' 任务

java - Jacoco tcpserver 重置转储

java - Jacoco 如何正确排除包

android - 从数据库中过滤 ArrayList

java - 开始在 Android 2.3 上编程