I am currently setting up Jacoco in an Android codebase that my team built. I don't have much experience in Android but I have set up Jacoco before in a Spring Boot codebase so that I can track the test coverage in Sonarqube. But I am having hard time doing in the Android codebase.
So the directory layout looks like this
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
I tried adding jacoco
in the 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")
}
I tried executing bash gradlew test jacocoTestReport
but it says
Task 'jacocoTestReport' not found in root project 'MyApp' and its subprojects.
I tried adding the line below in the MyApp/build.gradle.kts
, per JaCoco Plugin documentation (https://docs.gradle.org/current/userguide/jacoco_plugin.html)
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
}
but the output says something like below
Script compilation errors:
Line 12: tasks.test {
^ Unresolved reference: test
Line 13: finalizedBy(tasks.jacocoTestReport)
^ Unresolved reference: finalizedBy
I did similar configuration in a Spring Boot project before which ran normal. But here it cannot even detect task jacocoTestReport
.
What did I do wrong?