如何使用 Kotlin dsl gradle 启用代码覆盖率?

How to enable code coverage with Kotlin dsl gradle?

使用 gradle kotlin dsl 启用 jacoco 时失败,因为它查看 build.gradle 文件而不是 build.gradle.kts 文件。

这是错误:

##[warning]Unable to append code coverage data: Error: File or folder doesn't exist: /home/vsts/work/1/s/build.gradle
##[warning]Failed to enable code coverage: Unable to append code coverage data: Error: File or folder doesn't exist: /home/vsts/work/1/s/build.gradle

app/build.gradle.kts

plugins {
    jacoco
}

tasks.withType(JacocoReport::class.java).all {
    reports {
        xml.isEnabled = true
        xml.destination = File("$buildDir/reports/jacoco/report.xml")
    }
}

tasks.withType<Test> {
    jacoco {
        toolVersion = "0.8.3"
        reportsDir = file("$buildDir/reports/jacoco")
    }
    finalizedBy("jacocoTestReport")
}

Azure DevOps 任务

- task: Gradle@2
  displayName: Gradle Build
  inputs:
      gradleWrapperFile: 'gradlew'
      tasks: ':app:assembleDevDebug :networking:lintDebug :app:lintDevDebug --warning-mode all'
      publishJUnitResults: false
      testResultsFiles: '**/TEST-*.xml'
      javaHomeOption: 'JDKVersion'
      sonarQubeRunAnalysis: false
      codeCoverageToolOption: 'jaCoCo'

消息

##[warning]Unable to append code coverage data: Error: File or folder doesn't exist: /home/vsts/work/1/s/build.gradle
##[warning]Failed to enable code coverage: Unable to append code coverage data: Error: File or folder doesn't exist: /home/vsts/work/1/s/build.gradle

由于使用选项

AzureDevOps Gradle Task 生成
codeCoverageToolOption: 'jaCoCo'

在此任务的配置中,因为 AzureDevOps 的实施 Gradle 任务包含硬编码值 build.gradle:

引用 comment from the above bug ticket

The way for you to use code coverage would be to enable it in your build config files and publish them using another task - PublishCodeCoverageResults