Skip to content

Commit 3066455

Browse files
authored
Update: support for Android Gradle Plugin 7.3 (#68)
* Update: Dependencies and code for Android Gradle Plugin version 7.3 * Fix: check `enableUnitTestCoverage` and `enableAndroidTestCoverage` instead of the deprecated `isTestCoverageEnabled`
1 parent 4ce3c6c commit 3066455

File tree

10 files changed

+48
-57
lines changed

10 files changed

+48
-57
lines changed

gradle/libs.versions.toml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
[versions]
22
androidMinSdk = "19"
3-
androidTargetSdk = "31"
4-
androidCompileSdk = "31"
5-
kotlin = "1.5.31"
6-
androidGradlePlugin = "7.2.0"
3+
androidTargetSdk = "32"
4+
androidCompileSdk = "32"
5+
kotlin = "1.6.21"
6+
androidGradlePlugin = "7.3.0"
77

88
[libraries]
9-
appCompat = { module = "androidx.appcompat:appcompat", version = "1.4.0" }
10-
androidGradlePlugin = { module = "com.android.tools.build:gradle", version.ref = "androidGradlePlugin" }
11-
androidGradlePluginApi = { module = "com.android.tools.build:gradle-api", version.ref = "androidGradlePlugin" }
9+
appCompat = { module = "androidx.appcompat:appcompat", version = "1.5.0" }
10+
androidGradlePlugin = { module = "com.android.tools.build:gradle", version.ref = "androidGradlePlugin" }
11+
androidGradlePluginApi = { module = "com.android.tools.build:gradle-api", version.ref = "androidGradlePlugin" }
1212

1313
# Test dependencies
1414
junit = { module = "junit:junit", version = "4.13.2" }
@@ -18,8 +18,8 @@ espressoCore = { module = "androidx.test.espresso:espresso-core", vers
1818
androidJUnit = { module = "androidx.test.ext:junit", version = "1.1.3" }
1919
commonsCsv = { module = "org.apache.commons:commons-csv", version = "1.9.0" }
2020
kotlinTest = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
21-
robolectric = { module = "org.robolectric:robolectric", version = "4.7.3" }
22-
mockk = { module = "io.mockk:mockk", version = "1.12.4" }
21+
robolectric = { module = "org.robolectric:robolectric", version = "4.8.2" }
22+
mockk = { module = "io.mockk:mockk", version = "1.12.5" }
2323

2424
[bundles]
2525
androidInstrumentedTest = ["supportTestRunner", "espressoCore", "androidJUnit"]
@@ -31,7 +31,7 @@ kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "k
3131
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
3232
kotlinDokka = { id = "org.jetbrains.dokka", version.ref = "kotlin" }
3333
pluginPortalPublish = { id = "com.gradle.plugin-publish", version = "0.21.0" }
34-
mavenPublish = { id = "com.vanniktech.maven.publish", version = "0.19.0" }
34+
mavenPublish = { id = "com.vanniktech.maven.publish", version = "0.21.0" }
3535
versionCheck = { id = "com.github.ben-manes.versions", version = "0.42.0" }
3636
androidApp = { id = "com.android.application", version.ref = "androidGradlePlugin"}
3737
androidLibrary = { id = "com.android.library", version.ref = "androidGradlePlugin"}

plugin/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ jacocoTestReport {
3232
}
3333

3434
plugins.withId("com.vanniktech.maven.publish") {
35-
mavenPublish {
36-
sonatypeHost = "S01"
35+
mavenPublishing {
36+
publishToMavenCentral("S01")
37+
signAllPublications()
3738
}
3839
}
3940

plugin/src/main/kotlin/org/neotech/plugin/rootcoverage/JaCoCoConfiguration.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal fun RootCoveragePluginExtension.getFileFilterPatterns(): List<String> =
3535
internal fun RootCoveragePluginExtension.getBuildVariantFor(project: Project): String =
3636
buildVariantOverrides[project.path] ?: buildVariant
3737

38-
internal fun RootCoveragePluginExtension.getExecutionDataFileTree(project: Project): FileTree {
38+
internal fun Project.getExecutionDataFileTree(includeUnitTestResults: Boolean, includeAndroidTestResults: Boolean): FileTree? {
3939
val buildFolderPatterns = mutableListOf<String>()
4040
if (includeUnitTestResults) {
4141
// TODO instead of hardcoding this, obtain the location from the test tasks, something like this:
@@ -68,5 +68,9 @@ internal fun RootCoveragePluginExtension.getExecutionDataFileTree(project: Proje
6868
// Android Build Tools Plugin 7.1+
6969
buildFolderPatterns.add("outputs/code_coverage/*/connected/*/coverage.ec")
7070
}
71-
return project.fileTree(project.buildDir, includes = buildFolderPatterns)
71+
return if(buildFolderPatterns.isEmpty()) {
72+
null
73+
} else {
74+
fileTree(buildDir, includes = buildFolderPatterns)
75+
}
7276
}

plugin/src/main/kotlin/org/neotech/plugin/rootcoverage/RootCoveragePlugin.kt

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -148,64 +148,49 @@ class RootCoveragePlugin : Plugin<Project> {
148148
// Get the exact required build variant for the current sub-project.
149149
val buildVariant = rootProjectExtension.getBuildVariantFor(subProject)
150150

151-
152151
lateinit var buildTypes: NamedDomainObjectContainer<out BuildType>
153152
androidComponents.finalizeDsl { extension ->
154153
buildTypes = extension.buildTypes
155154
}
156155
androidComponents.onVariants { variant ->
157156
val buildType = buildTypes.find { it.name == variant.buildType }!!
158-
if (buildType.isTestCoverageEnabled && variant.name.replaceFirstChar(Char::titlecase) == buildVariant.replaceFirstChar(Char::titlecase)) {
159-
addSubProjectVariant(subProject, variant)
157+
if (variant.name.replaceFirstChar(Char::titlecase) == buildVariant.replaceFirstChar(Char::titlecase)) {
158+
if (buildType.enableAndroidTestCoverage || buildType.enableUnitTestCoverage || buildType.isTestCoverageEnabled) {
159+
addSubProjectVariant(subProject, variant, buildType)
160+
} else {
161+
subProject.logger.info("Note: Skipping code coverage for module '${subProject.name}', reason: BuildType $buildType has enableAndroidTestCoverage, enableUnitTestCoverage and testCoverageEnabled set to false, at least one of these must be true for code coverage to work.")
162+
}
160163
}
161164
}
162165
}
163166

164-
private fun JacocoReport.addSubProjectVariant(subProject: Project, variant: Variant) {
167+
private fun JacocoReport.addSubProjectVariant(subProject: Project, variant: Variant, buildType: BuildType) {
165168
val name = variant.name.replaceFirstChar(Char::titlecase)
166169

167170
// Gets the relative path from this task to the subProject
168171
val path = project.relativeProjectPath(subProject.path)
169172

170173
// Add dependencies to the test tasks of the subProject
171-
if (rootProjectExtension.shouldExecuteUnitTests()) {
174+
if (rootProjectExtension.shouldExecuteUnitTests() && (buildType.enableUnitTestCoverage || buildType.isTestCoverageEnabled)) {
172175
dependsOn("$path:test${name}UnitTest")
173176
}
174-
if (rootProjectExtension.shouldExecuteAndroidTests()) {
177+
if (rootProjectExtension.shouldExecuteAndroidTests() && (buildType.enableAndroidTestCoverage || buildType.isTestCoverageEnabled)) {
175178
dependsOn("$path:connected${name}AndroidTest")
176179
}
177180

178-
179-
// Start temporary fix for AGP 7.2
180-
// For some reason `variant.sources.java.all` causes BuildConfig files to go missing
181-
// See: https://github.com/NeoTech-Software/Android-Root-Coverage-Plugin/issues/54
182-
val baseVariant = when(val androidExtension = subProject.extensions.findByName("android")) {
183-
is LibraryExtension -> androidExtension.libraryVariants
184-
is AppExtension -> androidExtension.applicationVariants
185-
else -> {
186-
subProject.logger.warn(
187-
"Note: Skipping code coverage for module '${subProject.name}', currently the" +
188-
" RootCoveragePlugin only supports Android Library and App Modules.")
189-
return
190-
}
191-
}
192-
baseVariant.all {
193-
if(name.contentEquals(it.baseName, ignoreCase = true)) {
194-
val sourceFiles = it.getSourceFolders(SourceKind.JAVA).map { file -> file.dir }
195-
sourceDirectories.from(subProject.files(sourceFiles))
196-
}
197-
}
198-
// End temporary fix for AGP 7.2
199-
200-
// Working code in AGP 7.3-beta01:
201-
// sourceDirectories.from(variant.sources.java?.all)
181+
sourceDirectories.from(variant.sources.java?.all)
202182

203183
classDirectories.from(variant.artifacts.getAll(MultipleArtifact.ALL_CLASSES_DIRS).map {
204184
it.map { directory ->
205185
subProject.fileTree(directory.asFile, excludes = rootProjectExtension.getFileFilterPatterns())
206186
}
207187
})
208-
executionData.from(rootProjectExtension.getExecutionDataFileTree(subProject))
188+
executionData.from(
189+
subProject.getExecutionDataFileTree(
190+
includeUnitTestResults = rootProjectExtension.includeUnitTestResults && (buildType.enableUnitTestCoverage || buildType.isTestCoverageEnabled),
191+
includeAndroidTestResults = rootProjectExtension.includeAndroidTestResults && (buildType.enableAndroidTestCoverage || buildType.isTestCoverageEnabled)
192+
)
193+
)
209194
}
210195

211196
/**

plugin/src/main/kotlin/org/neotech/plugin/rootcoverage/utilities/AndroidGradlePluginExtensions.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ fun Project.onVariant(variantName: String, action: (variant: Variant?) -> Unit){
2828
}
2929
}
3030

31-
32-
3331
fun Project.afterAndroidPluginApplied(notFoundAction: () -> Unit = {}, action: (AppliedPlugin) -> Unit) {
3432
var didExecuteBlock = false
3533
pluginManager.withPlugin(ANDROID_APPLICATION_PLUGIN_ID) {

plugin/src/test/kotlin/org/neotech/plugin/rootcoverage/IntegrationTest.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import org.neotech.plugin.rootcoverage.util.createLocalPropertiesFile
1313
import org.neotech.plugin.rootcoverage.util.put
1414
import java.io.File
1515
import java.util.Properties
16-
import kotlin.test.assertEquals
1716

1817
@RunWith(Parameterized::class)
1918
class IntegrationTest(
@@ -60,10 +59,10 @@ class IntegrationTest(
6059
}
6160

6261
private fun BuildResult.assertRootCoverageReport(file: File) {
63-
assertEquals(task(":rootCoverageReport")!!.outcome, TaskOutcome.SUCCESS)
62+
assertThat(task(":rootCoverageReport")!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
6463

6564
// Also check if the old task name is still exe
66-
assertEquals(task(":rootCodeCoverageReport")!!.outcome, TaskOutcome.SUCCESS)
65+
assertThat(task(":rootCodeCoverageReport")!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
6766

6867
val report = CoverageReport.from(file)
6968

@@ -75,7 +74,7 @@ class IntegrationTest(
7574
}
7675

7776
private fun BuildResult.assertAppCoverageReport() {
78-
assertEquals(task(":app:coverageReport")!!.outcome, TaskOutcome.SUCCESS)
77+
assertThat(task(":app:coverageReport")!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
7978

8079
val report = CoverageReport.from(File(projectRoot, "app/build/reports/jacoco.csv"))
8180

@@ -86,7 +85,7 @@ class IntegrationTest(
8685
}
8786

8887
private fun BuildResult.assertAndroidLibraryCoverageReport() {
89-
assertEquals(task(":library_android:coverageReport")!!.outcome, TaskOutcome.SUCCESS)
88+
assertThat(task(":library_android:coverageReport")!!.outcome).isEqualTo(TaskOutcome.SUCCESS)
9089

9190
val report = CoverageReport.from(File(projectRoot, "library_android/build/reports/jacoco.csv"))
9291

@@ -127,7 +126,7 @@ class IntegrationTest(
127126

128127
val testFixtures = File("src/test/test-fixtures").listFiles()?.filter { it.isDirectory }
129128
?: error("Could not list test fixture directories")
130-
val gradleVersions = arrayOf("7.3.3", "7.4", "7.4.2", "7.5-rc-1")
129+
val gradleVersions = arrayOf("7.4", "7.4.2", "7.5.1")
131130
return testFixtures.flatMap { file ->
132131
gradleVersions.map { gradleVersion ->
133132
arrayOf("${file.name}-$gradleVersion", file, gradleVersion)

plugin/src/test/test-fixtures/multi-module/app/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ plugins {
55

66
android {
77

8+
namespace "org.neotech.app"
89
compileSdkVersion libs.versions.androidCompileSdk.get().toInteger()
10+
911
defaultConfig {
1012
applicationId "org.neotech.app"
1113
minSdkVersion libs.versions.androidMinSdk.get().toInteger()
@@ -17,7 +19,8 @@ android {
1719

1820
buildTypes {
1921
debug {
20-
testCoverageEnabled true
22+
enableUnitTestCoverage true
23+
enableAndroidTestCoverage true
2124
}
2225
release {
2326
minifyEnabled false

plugin/src/test/test-fixtures/multi-module/app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest
33
xmlns:android="http://schemas.android.com/apk/res/android"
4-
package="org.neotech.app"
54
>
65

76
<application>

plugin/src/test/test-fixtures/multi-module/library_android/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ plugins {
55

66
android {
77

8+
namespace "org.neotech.library.a"
89
compileSdkVersion libs.versions.androidCompileSdk.get().toInteger()
10+
911
defaultConfig {
1012
minSdkVersion libs.versions.androidMinSdk.get().toInteger()
1113
targetSdkVersion libs.versions.androidTargetSdk.get().toInteger()
@@ -17,6 +19,7 @@ android {
1719

1820
buildTypes {
1921
debug {
22+
2023
testCoverageEnabled true
2124
}
2225
release {
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="org.neotech.library.a" />
1+
<manifest />

0 commit comments

Comments
 (0)