diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 9e68ffa7d..2fe18645e 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -21,7 +21,6 @@ plugins { alias(libs.plugins.nowinandroid.android.application.flavors) alias(libs.plugins.nowinandroid.android.application.jacoco) alias(libs.plugins.nowinandroid.android.hilt) - id("jacoco") alias(libs.plugins.nowinandroid.android.application.firebase) id("com.google.android.gms.oss-licenses-plugin") alias(libs.plugins.baselineprofile) diff --git a/build-logic/convention/src/main/kotlin/AndroidApplicationJacocoConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidApplicationJacocoConventionPlugin.kt index 4c3acc520..75ca6aabb 100644 --- a/build-logic/convention/src/main/kotlin/AndroidApplicationJacocoConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/AndroidApplicationJacocoConventionPlugin.kt @@ -14,8 +14,7 @@ * limitations under the License. */ -import com.android.build.api.variant.ApplicationAndroidComponentsExtension -import com.google.samples.apps.nowinandroid.configureJacoco +import com.android.build.gradle.internal.dsl.BaseAppModuleExtension import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.kotlin.dsl.getByType @@ -23,13 +22,13 @@ import org.gradle.kotlin.dsl.getByType class AndroidApplicationJacocoConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { - with(pluginManager) { - apply("org.gradle.jacoco") - apply("com.android.application") + val androidExtension = extensions.getByType() + + androidExtension.buildTypes.forEach { buildType -> + buildType.enableAndroidTestCoverage = true + buildType.enableUnitTestCoverage = true } - val extension = extensions.getByType() - configureJacoco(extension) } } -} \ No newline at end of file +} diff --git a/build-logic/convention/src/main/kotlin/AndroidLibraryJacocoConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidLibraryJacocoConventionPlugin.kt index 86ca091c3..1aed3e990 100644 --- a/build-logic/convention/src/main/kotlin/AndroidLibraryJacocoConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/AndroidLibraryJacocoConventionPlugin.kt @@ -14,8 +14,8 @@ * limitations under the License. */ +import com.android.build.api.dsl.LibraryExtension import com.android.build.api.variant.LibraryAndroidComponentsExtension -import com.google.samples.apps.nowinandroid.configureJacoco import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.kotlin.dsl.getByType @@ -23,13 +23,17 @@ import org.gradle.kotlin.dsl.getByType class AndroidLibraryJacocoConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { - with(pluginManager) { - apply("org.gradle.jacoco") - apply("com.android.library") + val androidExtension: LibraryExtension = extensions.getByType() + + androidExtension.buildTypes.forEach { buildType -> + // The jacoco plugin is applied automatically when any of these are set + buildType.enableAndroidTestCoverage = true + buildType.enableUnitTestCoverage = true } + val extension = extensions.getByType() - configureJacoco(extension) + } } -} \ No newline at end of file +} diff --git a/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/Jacoco.kt b/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/Jacoco.kt deleted file mode 100644 index 596c4f579..000000000 --- a/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/Jacoco.kt +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.samples.apps.nowinandroid - -import com.android.build.api.variant.AndroidComponentsExtension -import org.gradle.api.Project -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*.*" -) - -private fun String.capitalize() = replaceFirstChar { - if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() -} - -internal fun Project.configureJacoco( - androidComponentsExtension: AndroidComponentsExtension<*, *, *>, -) { - configure { - toolVersion = libs.findVersion("jacoco").get().toString() - } - - val jacocoTestReport = tasks.create("jacocoTestReport") - - androidComponentsExtension.onVariants { variant -> - val testTaskName = "test${variant.name.capitalize()}UnitTest" - val buildDir = layout.buildDirectory.get().asFile - val reportTask = tasks.register("jacoco${testTaskName.capitalize()}Report", JacocoReport::class) { - 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) - } - - tasks.withType().configureEach { - configure { - // Required for JaCoCo + Robolectric - // https://github.com/robolectric/robolectric/issues/2230 - // TODO: Consider removing if not we don't add Robolectric - isIncludeNoLocationClasses = true - - // Required for JDK 11 with the above - // https://github.com/gradle/gradle/issues/5184#issuecomment-391982009 - excludes = listOf("jdk.internal.*") - } - } -}