Cannot add task ':jacocoTestReport' as a t

2019-07-26 23:41发布

问题:

I am trying to add the following task so that I can get some coverage data in my java + kotlin project (for what it is worth, this is a gradle project)... but I get the following error :

"Cannot add task ':jacocoTestReport' as a task with that name already exists"

Here is the actual task I am trying to add :

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebugUnitTest") {
group = "Reporting"
description = "Generate Jacoco coverage reports for Debug build"

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

// what to exclude from coverage report
// UI, "noise", generated classes, platform classes, etc.
def excludes = [
        '**/R.class',
        '**/R$*.class',
        '**/*$ViewInjector*.*',
        '**/BuildConfig.*',
        '**/Manifest*.*',
        '**/*Test*.*',
        'android/**/*.*',
        '**/*Fragment.*',
        '**/*Activity.*'
]
// generated classes
classDirectories = fileTree(
        dir: "$buildDir/intermediates/classes/debug",
        excludes: excludes
) + fileTree(
        dir: "$buildDir/tmp/kotlin-classes/debug",
        excludes: excludes
)

// sources
sourceDirectories = files([
        android.sourceSets.main.java.srcDirs,
        "src/main/kotlin"
])
    executionData = files("$buildDir/jacoco/testDebugUnitTest.exec")
}

Now, the issue I am confused about here, is that I can't find another class of this name anywhere... so perhaps there is something funky going on? I have tried googling this, but haven't really been able to find anything which truly helps me solve the problem.

All help greatly appreciated. I realize this is not a java or kotlin specific problem - but since it is a joint java + kotlin project, I thought I would tag both in this question, in case there is some nuanced issue that somebody else has seen.

回答1:

Assuming you're already applying the Jacoco Gradle plugin, then yes, it already defines a task called jacocoTestReport, hence the error.

All you need to do is define your specific settings as per the documentation https://docs.gradle.org/current/userguide/jacoco_plugin.html#sec:jacoco_report_configuration

an example is below:

jacocoTestReport {
  dependsOn "testDebugUnitTest"
    reports {
        xml.enabled = true
        html.enabled = true
    }
}

Most of the other configuration items you've listed belong in the 'jacoco' configuration block. https://docs.gradle.org/current/userguide/jacoco_plugin.html#sec:jacoco_specific_task_configuration