java Gradle Jacoco - 找不到方法 jacocoTestReport()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43871349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:47:12  来源:igfitidea点击:

Gradle Jacoco - Could not find method jacocoTestReport()

javaandroidgradlejacoco

提问by Guido

I'm trying to generate a Jacoco test report in Gradle. When I try to sync my code, I will receive the following error:

我正在尝试在 Gradle 中生成 Jacoco 测试报告。当我尝试同步我的代码时,我会收到以下错误:

Error:(56, 0) Could not find method jacocoTestReport() for arguments [build_38ehqsoyd54r3n1gzrop303so$_run_closure4@10012308] on project ':app' of type org.gradle.api.Project.

错误:(56, 0) 无法在 org.gradle.api.Project 类型的项目“:app”上找到参数 [build_38ehqsoyd54r3n1gzrop303so$_run_closure4@10012308] 的方法 jacocoTestReport()。

My build.gradle file contains the following items:

我的 build.gradle 文件包含以下项目:

apply plugin: 'jacoco'

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

jacocoTestReport {
    group = "Reporting"
    reports {
        xml.enabled true
        csv.enabled false
        html.destination "${buildDir}/reports/coverage"
    }
}

When I look at the documentation , I don't see anything which I'm doing wrong.

当我查看文档时,我没有看到任何我做错的地方。

Gradle version: 3.3

摇篮版本:3.3

Why am I receiving this error and how can I fix it?

为什么我会收到此错误,我该如何解决?

采纳答案by Christopher

Basically I know two ways to achieve this.

基本上我知道两种方法来实现这一目标。

The first approach is the built-in android gradle plugin feature:

第一种方法是内置的android gradle插件功能:

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

This one will define gradle tasks, which can be executed. As far as I know this works fine with instrumentation tests. More information: Code Coverage on Android

这将定义可以执行的 gradle 任务。据我所知,这适用于仪器测试。更多信息:Android 上的代码覆盖率

The 2nd approach is to use this plugin:

第二种方法是使用这个插件:

https://github.com/vanniktech/gradle-android-junit-jacoco-plugin

https://github.com/vanniktech/gradle-android-junit-jacoco-plugin

Setup is easy:

设置很简单:

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

apply plugin: 'com.vanniktech.android.junit.jacoco'

And after project sync, you will have tasks like jacocoTestReport<Flavor><BuildType>

项目同步后,您将有类似的任务 jacocoTestReport<Flavor><BuildType>

We use this to measure the code coverage of our unit tests running on the local machine.

我们用它来衡量在本地机器上运行的单元测试的代码覆盖率。

回答by Godin

As stated in documentation mentioned in your question:

您的问题中提到的文档所述

If the Java plugin is also applied to your project, a new task named jacocoTestReport is created that depends on the test task.

如果 Java 插件也应用于您的项目,则会创建一个名为 jacocoTestReport 的新任务,该任务依赖于测试任务。

what is pretty logical - measurement of coverage for Java code requires its compilation, execution of tests, etc.

什么是合乎逻辑的 - 测量 Java 代码的覆盖率需要编译、执行测试等。

So that indeed usage of your example of build.gradlecauses failure, which disappears after addition of apply plugin: 'java'.

因此,确实使用您的示例build.gradle导致失败,添加apply plugin: 'java'.