使用 Gadle kotlin 为多模块 Android 代码库设置 jacoco

问题描述 投票:0回答:0

我目前正在我的团队构建的 Android 代码库中设置 Jacoco。我在 Android 方面没有太多经验,但我之前已经在 Spring Boot 代码库中设置了 Jacoco,以便我可以跟踪 Sonarqube 中的测试覆盖率。但我在 Android 代码库中遇到了困难。

所以目录布局看起来像这样

MyApp/
├─ app/
│  ├─ build/
│  ├─ src/
│  ├─ build.gradle.kts
├─ buildSrc/
│  ├─ build.gradle.kts
├─ modules/
│  ├─ module1/
│  │  ├─ src/
│  │  ├─ build.gradle.kts
│  ├─ module2/
│  │  ├─ src/
│  │  ├─ build.gradle.kts
├─ build.gradle.kts
├─ gradle.properties
├─ gradlew
├─ settings.gradle.kts

我尝试在

jacoco
中添加
MyApp/build.gradle.kts

plugins {
    id(Dependencies.Plugins.androidApplication) version Dependencies.Versions.androidAppplication apply false
    id(Dependencies.Plugins.androidLibrary) version Dependencies.Versions.androidLibrary apply false
    id(Dependencies.Plugins.hilt) version Dependencies.Versions.hilt apply false
    id(Dependencies.Plugins.openApi) version Dependencies.Versions.openApi
    id(Dependencies.Plugins.kotlinAndroid) version Dependencies.Versions.kotlinAndroid apply false
    id(Dependencies.Plugins.sonarqube) version Dependencies.Versions.sonarqube
    
    id("jacoco")
}

我尝试执行

bash gradlew test jacocoTestReport
但它说

Task 'jacocoTestReport' not found in root project 'MyApp' and its subprojects.

我尝试根据 JaCoco 插件文档 (

https://docs.gradle.org/current/userguide/jacoco_plugin.html
) 在MyApp/build.gradle.kts中添加以下行

tasks.test {
    finalizedBy(tasks.jacocoTestReport) // report is always generated after tests run
}
tasks.jacocoTestReport {
    dependsOn(tasks.test) // tests are required to run before generating the report
}

但输出如下所示

Script compilation errors:

  Line 12: tasks.test {
                 ^ Unresolved reference: test

  Line 13:     finalizedBy(tasks.jacocoTestReport)                                              
               ^ Unresolved reference: finalizedBy

我之前在Spring Boot项目中做过类似的配置,运行正常。但在这里它甚至无法检测任务

jacocoTestReport

我做错了什么?

android kotlin gradle jacoco android-jacoco
© www.soinside.com 2019 - 2024. All rights reserved.