-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5eb422f
commit b6bc0fb
Showing
16 changed files
with
236 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
build-logic/convention/src/main/kotlin/AndroidApplicationJacocoConventionPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import com.android.build.api.variant.ApplicationAndroidComponentsExtension | ||
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension | ||
import com.android254.configureJacoco | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.kotlin.dsl.getByType | ||
|
||
class AndroidApplicationJacocoConventionPlugin : Plugin<Project> { | ||
override fun apply(target: Project) { | ||
with(target) { | ||
pluginManager.apply("jacoco") | ||
val androidExtension = extensions.getByType<BaseAppModuleExtension>() | ||
|
||
androidExtension.buildTypes.configureEach { | ||
enableAndroidTestCoverage = true | ||
enableUnitTestCoverage = true | ||
} | ||
|
||
configureJacoco(extensions.getByType<ApplicationAndroidComponentsExtension>()) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
build-logic/convention/src/main/kotlin/AndroidLibraryJacocoConventionPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import com.android.build.api.dsl.LibraryExtension | ||
import com.android.build.api.variant.LibraryAndroidComponentsExtension | ||
import com.android254.configureJacoco | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.kotlin.dsl.getByType | ||
|
||
class AndroidLibraryJacocoConventionPlugin : Plugin<Project> { | ||
override fun apply(target: Project) { | ||
with(target) { | ||
pluginManager.apply("jacoco") | ||
val androidExtension = extensions.getByType<LibraryExtension>() | ||
|
||
androidExtension.buildTypes.configureEach { | ||
enableAndroidTestCoverage = true | ||
enableUnitTestCoverage = true | ||
} | ||
|
||
configureJacoco(extensions.getByType<LibraryAndroidComponentsExtension>()) | ||
} | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
build-logic/convention/src/main/kotlin/com/android254/Jacoco.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.android254 | ||
|
||
import com.android.build.api.artifact.ScopedArtifact | ||
import com.android.build.api.variant.AndroidComponentsExtension | ||
import com.android.build.api.variant.ScopedArtifacts | ||
import org.gradle.api.Project | ||
import org.gradle.api.file.Directory | ||
import org.gradle.api.file.RegularFile | ||
import org.gradle.api.provider.ListProperty | ||
import org.gradle.api.tasks.testing.Test | ||
import org.gradle.kotlin.dsl.configure | ||
import org.gradle.kotlin.dsl.register | ||
import org.gradle.kotlin.dsl.withType | ||
import org.gradle.testing.jacoco.plugins.JacocoPluginExtension | ||
import org.gradle.testing.jacoco.plugins.JacocoTaskExtension | ||
import org.gradle.testing.jacoco.tasks.JacocoReport | ||
import java.util.Locale | ||
|
||
private val coverageExclusions = listOf( | ||
// Android | ||
"**/R.class", | ||
"**/R\$*.class", | ||
"**/BuildConfig.*", | ||
"**/Manifest*.*", | ||
"**/*_Hilt*.class", | ||
"**/Hilt_*.class", | ||
) | ||
|
||
private fun String.capitalize() = replaceFirstChar { | ||
if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() | ||
} | ||
|
||
/** | ||
* Creates a new task that generates a combined coverage report with data from local and | ||
* instrumented tests. | ||
* | ||
* `create{variant}CombinedCoverageReport` | ||
* | ||
* Note that coverage data must exist before running the task. This allows us to run device | ||
* tests on CI using a different Github Action or an external device farm. | ||
*/ | ||
internal fun Project.configureJacoco( | ||
androidComponentsExtension: AndroidComponentsExtension<*, *, *>, | ||
) { | ||
configure<JacocoPluginExtension> { | ||
toolVersion = libs.findVersion("jacoco").get().toString() | ||
} | ||
|
||
androidComponentsExtension.onVariants { variant -> | ||
val myObjFactory = project.objects | ||
val buildDir = layout.buildDirectory.get().asFile | ||
val allJars: ListProperty<RegularFile> = myObjFactory.listProperty(RegularFile::class.java) | ||
val allDirectories: ListProperty<Directory> = myObjFactory.listProperty(Directory::class.java) | ||
val reportTask = | ||
tasks.register("create${variant.name.capitalize()}CombinedCoverageReport", JacocoReport::class) { | ||
|
||
classDirectories.setFrom( | ||
allJars, | ||
allDirectories.map { dirs -> | ||
dirs.map { dir -> | ||
myObjFactory.fileTree().setDir(dir).exclude(coverageExclusions) | ||
} | ||
} | ||
) | ||
reports { | ||
xml.required.set(true) | ||
html.required.set(true) | ||
} | ||
|
||
// TODO: This is missing files in src/debug/, src/prod, src/demo, src/demoDebug... | ||
sourceDirectories.setFrom(files("$projectDir/src/main/java", "$projectDir/src/main/kotlin")) | ||
|
||
executionData.setFrom( | ||
project.fileTree("$buildDir/outputs/unit_test_code_coverage/${variant.name}UnitTest") | ||
.matching { include("**/*.exec") }, | ||
|
||
project.fileTree("$buildDir/outputs/code_coverage/${variant.name}AndroidTest") | ||
.matching { include("**/*.ec") } | ||
) | ||
} | ||
|
||
|
||
variant.artifacts.forScope(ScopedArtifacts.Scope.PROJECT) | ||
.use(reportTask) | ||
.toGet( | ||
ScopedArtifact.CLASSES, | ||
{ _ -> allJars }, | ||
{ _ -> allDirectories }, | ||
) | ||
} | ||
|
||
tasks.withType<Test>().configureEach { | ||
configure<JacocoTaskExtension> { | ||
// Required for JaCoCo + Robolectric | ||
// https://github.com/robolectric/robolectric/issues/2230 | ||
isIncludeNoLocationClasses = true | ||
|
||
// Required for JDK 11 with the above | ||
// https://github.com/gradle/gradle/issues/5184#issuecomment-391982009 | ||
excludes = listOf("jdk.internal.*") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
codecov: | ||
notify: | ||
wait_for_ci: true | ||
max_report_age: off | ||
require_ci_to_pass: true | ||
comment: | ||
behavior: default | ||
layout: "reach, diff, flags, files" | ||
show_carryforward_flags: false | ||
coverage: | ||
precision: 1 | ||
status: | ||
changes: false | ||
patch: | ||
default: | ||
target: 60.0 | ||
project: | ||
default: | ||
enabled: true | ||
target: 25.0 | ||
flag_management: | ||
default_rules: | ||
carryforward: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.