From 67290500a9c4d892f3701559ed29d6d24c872431 Mon Sep 17 00:00:00 2001 From: Alex Vanyo Date: Fri, 10 Dec 2021 16:50:55 -0800 Subject: [PATCH] Setup jacoco coverage reporting Change-Id: I02bc2e54723179afd921ffe7b2537e035e5906ed --- app/build.gradle | 56 +++++++++++++++++++++++++++++++++++++++ gradle/libs.versions.toml | 1 + 2 files changed, 57 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index c3ad9ac6c..38854cbb0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,8 +17,11 @@ plugins { id 'com.android.application' id 'kotlin-android' id 'kotlinx-serialization' + id 'jacoco' } +def jacocoTestReport = tasks.create("jacocoTestReport") + android { compileSdk buildConfig.compileSdk @@ -66,6 +69,42 @@ android { includeAndroidResources = true } } + androidComponents { + def coverageExclusions = [ + // Android + "**/R.class", + "**/R\$*.class", + "**/BuildConfig.*", + "**/Manifest*.*" + ] + + onVariants( + selector().all(), + { variant -> + def testTaskName = "test${variant.name.capitalize()}UnitTest" + + def reportTask = tasks.register("jacoco${testTaskName.capitalize()}Report", JacocoReport) { + dependsOn(testTaskName) + + reports { + xml.required.set(true) + html.required.set(true) + } + + classDirectories.setFrom( + fileTree("$buildDir/tmp/kotlin-classes/${variant.name}") { + exclude(coverageExclusions) + } + ) + + sourceDirectories.setFrom(files("$projectDir/src/main/java", "$projectDir/src/main/kotlin")) + executionData.setFrom(file("$buildDir/jacoco/${testTaskName}.exec")) + } + + jacocoTestReport.dependsOn(reportTask) + } + ) + } } dependencies { @@ -115,3 +154,20 @@ dependencies { } } } + +jacoco { + toolVersion libs.versions.jacoco.get() +} + +tasks.withType(Test) { + jacoco { + // Required for JaCoCo + Robolectric + // https://github.com/robolectric/robolectric/issues/2230 + // TODO: Consider removing if not we don't add Robolectric + setIncludeNoLocationClasses(true) + + // Required for JDK 11+ with the above + // https://github.com/gradle/gradle/issues/5184#issuecomment-391982009 + excludes = ["jdk.internal.*"] + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7dffcafd9..bf4883cbd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -12,6 +12,7 @@ androidxLifecycle = "2.4.0" androidxNavigation = "2.3.5" androidxTest = "1.4.0" androidxTestExt = "1.1.2" +jacoco = "0.8.7" junit4 = "4.13" kotlin = "1.6.0" kotlinxCoroutines = "1.5.2"