Skip to content

Commit 7ee6e76

Browse files
committed
Add and setup code coverage: jacoco plugin.
1 parent b36251b commit 7ee6e76

File tree

4 files changed

+74
-14
lines changed

4 files changed

+74
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Here are some useful Gradle/adb commands for executing this example:
5454
* `./gradlew compileApp` - Builds the debug apk.
5555
* `./gradlew runUnitTests` - Execute unit tests (both unit and integration).
5656
* `./gradlew runAcceptanceTests` - Execute acceptance and instrumentation tests in the connected device.
57+
* `./gradlew runTestCoverage` - Reports code coverage on tests within the Android codebase.
5758

5859
## Discussions
5960
Refer to the issues section: https://github.com/android10/Android-CleanArchitecture-Kotlin/issues

buildSrc/src/main/kotlin/scripts/infrastructure.gradle.kts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@ package scripts
33
import scripts.Variants_gradle.*
44
import java.util.*
55

6-
private object Default {
7-
const val BUILD_TYPE = BuildTypes.DEBUG
8-
const val BUILD_FLAVOR = ProductFlavors.DEV
9-
10-
val VARIANT = "${BUILD_FLAVOR.capitalize()}${BUILD_TYPE.capitalize()}"
11-
}
12-
136
tasks.register("clean", Delete::class){
147
delete(rootProject.buildDir)
158
}
@@ -21,22 +14,22 @@ tasks.named<Wrapper>("wrapper") {
2114

2215
tasks.register("runUnitTests") {
2316
description = "Runs all Unit Tests."
24-
dependsOn(":app:test${Default.VARIANT}UnitTest")
17+
dependsOn(":app:test${Default.BUILD_VARIANT}UnitTest")
2518
}
2619

2720
tasks.register("runAcceptanceTests") {
2821
description = "Runs all Acceptance Tests in the connected device."
29-
dependsOn(":app:connected${Default.VARIANT}AndroidTest")
22+
dependsOn(":app:connected${Default.BUILD_VARIANT}AndroidTest")
3023
}
3124

3225
tasks.register("compileApp") {
3326
description = "Compiles the Clean Architecture Android Client."
34-
dependsOn(":app:assemble${Default.VARIANT}")
27+
dependsOn(":app:assemble${Default.BUILD_VARIANT}")
3528
}
3629

3730
tasks.register("runApp", Exec::class) {
3831
val compileAppTask = "compileApp"
39-
val installAppTask = ":app:install${Default.VARIANT}"
32+
val installAppTask = ":app:install${Default.BUILD_VARIANT}"
4033

4134
description = "Compiles and runs the Clean Architecture Android Client in the connected device."
4235
dependsOn(compileAppTask, installAppTask)

buildSrc/src/main/kotlin/scripts/quality.gradle.kts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package scripts
22

3-
plugins { id("com.android.application") apply false }
3+
import scripts.Variants_gradle.*
4+
5+
plugins {
6+
id("com.android.application") apply false
7+
id("jacoco")
8+
}
49

510
android {
611
lintOptions {
@@ -12,4 +17,58 @@ android {
1217
disable("IconDensities") //For testing purpose. This is safe to remove.
1318
disable("IconMissingDensityFolder") //For testing purpose. This is safe to remove.
1419
}
20+
}
21+
22+
tasks.register("jacocoReport", JacocoReport::class) {
23+
group = "Quality"
24+
description = "Report code coverage on tests within the Android codebase."
25+
dependsOn("test${Default.BUILD_VARIANT}UnitTest")
26+
27+
val buildVariantClassPath = "${Default.BUILD_FLAVOR}${Default.BUILD_TYPE.capitalize()}"
28+
val outputDir = "${project.buildDir}/testCoverage/html"
29+
30+
reports {
31+
xml.isEnabled = true
32+
html.isEnabled = true
33+
html.destination = file(outputDir)
34+
}
35+
36+
classDirectories.setFrom(fileTree(project.buildDir) {
37+
include(
38+
"**/classes/**/main/**",
39+
"**/intermediates/classes/$buildVariantClassPath/**",
40+
"**/intermediates/javac/$buildVariantClassPath/*/classes/**",
41+
"**/tmp/kotlin-classes/$buildVariantClassPath/**")
42+
exclude(
43+
"**/R.class",
44+
"**/R\$*.class",
45+
"**/BuildConfig.*",
46+
"**/Manifest*.*",
47+
"**/Manifest$*.class",
48+
"**/*Test*.*",
49+
"**/Injector.*",
50+
"android/**/*.*",
51+
"**/*\$Lambda$*.*",
52+
"**/*\$inlined$*.*",
53+
"**/di/*.*",
54+
"**/*Database.*",
55+
"**/*Response.*",
56+
"**/*Application.*",
57+
"**/*Entity.*")
58+
}
59+
)
60+
sourceDirectories.setFrom(fileTree(project.projectDir) {
61+
include("src/main/java/**", "src/main/kotlin/**") })
62+
executionData.setFrom(fileTree(project.buildDir) {
63+
include("**/*.exec", "**/*.ec") })
64+
65+
doLast {
66+
println("Code Coverage Report: $outputDir/index.html")
67+
}
68+
}
69+
70+
tasks.register("runTestCoverage") {
71+
group = "Quality"
72+
description = "Report code coverage on tests within the Android codebase."
73+
dependsOn("jacocoReport")
1574
}

buildSrc/src/main/kotlin/scripts/variants.gradle.kts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package scripts
22

33
plugins { id("com.android.application") apply false }
44

5-
object BuildTypes {
5+
private object BuildTypes {
66
const val DEBUG = "debug"
77
const val RELEASE = "release"
88
}
99

10-
object ProductFlavors {
10+
private object ProductFlavors {
1111
const val DEV = "dev"
1212
const val INTERNAL = "internal"
1313
const val PUBLIC = "public"
@@ -17,6 +17,13 @@ private object FlavorDimensions {
1717
const val DEFAULT = "default"
1818
}
1919

20+
object Default {
21+
const val BUILD_TYPE = BuildTypes.DEBUG
22+
const val BUILD_FLAVOR = ProductFlavors.DEV
23+
24+
val BUILD_VARIANT = "${BUILD_FLAVOR.capitalize()}${BUILD_TYPE.capitalize()}"
25+
}
26+
2027
android {
2128
buildTypes {
2229
getByName(BuildTypes.DEBUG) {

0 commit comments

Comments
 (0)