Skip to content

Commit d6f59b5

Browse files
authored
Remove outdated workarounds & support AGP 3.5.0-alpha12 and beyond (#170)
* Update internal AGP version to 3.4.0 stable * Update to Gradle 5.4 * Use 3.4.0 stable for the Functional Tests * Remove outdated interop workarounds for Java artifact files This will achieve compatibility with AGP versions past 3.5.0-alpha12 * Remove more outdated interop workarounds * Remove more interop workarounds that don’t need to be there any longer * Bump Functional Tests for AGP 3.5.x to the latest alpha * Apply Jacoco plugin to the base AGP Functional Tests This can catch incompatibilities in the future, just like the one related to javaOutputDir * Bump minimal supported AGP version to 3.2.0
1 parent 6ab94aa commit d6f59b5

File tree

15 files changed

+66
-134
lines changed

15 files changed

+66
-134
lines changed

buildSrc/src/main/kotlin/Versions.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ object Versions {
1515

1616
const val aapt2: String = "3.2.1-4818971"
1717

18-
const val com_android_tools_build_gradle: String = "3.4.0-rc03"
18+
const val com_android_tools_build_gradle: String = "3.4.0"
1919
const val com_android_tools_build_gradle_32x: String = "3.2.1"
2020
const val com_android_tools_build_gradle_33x: String = "3.3.2"
21-
const val com_android_tools_build_gradle_34x: String = "3.4.0-rc03"
22-
const val com_android_tools_build_gradle_35x: String = "3.5.0-alpha07"
21+
const val com_android_tools_build_gradle_34x: String = "3.4.0"
22+
const val com_android_tools_build_gradle_35x: String = "3.5.0-alpha13"
2323

2424
const val lint_gradle: String = "26.2.1"
2525

gradle/wrapper/gradle-wrapper.jar

426 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

plugin/android-junit5/src/main/groovy/de/mannodermaus/gradle/plugins/junit5/GroovyInterop.groovy

Lines changed: 16 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,24 @@ package de.mannodermaus.gradle.plugins.junit5
22

33
import com.android.annotations.NonNull
44
import com.android.build.gradle.api.BaseVariant
5+
import com.android.build.gradle.internal.scope.InternalArtifactType
56
import com.android.build.gradle.internal.scope.VariantScope
67
import com.android.build.gradle.internal.variant.BaseVariantData
7-
import com.android.build.gradle.tasks.factory.AndroidUnitTest
8-
import com.annimon.stream.Optional
8+
import com.android.builder.core.VariantTypeImpl
99
import org.gradle.api.Project
1010
import org.gradle.api.file.ConfigurableFileCollection
1111
import org.gradle.testing.jacoco.tasks.JacocoReportBase
1212

13-
import javax.annotation.Nullable
14-
1513
/**
1614
* Utility functions exposed to Kotlin consumers
1715
* that can't safely access Groovy members otherwise,
1816
* or require reflection to access in a compatible manner
1917
* across all supported versions of the Android Gradle Plugin.*/
2018
class GroovyInterop {
2119

22-
// Access to UNIT_TEST was moved from VariantType to VariantTypeImpl in AGP 3.2.0-alpha06
23-
private static final def VariantType =
24-
reflectiveClass("com.android.builder.core.VariantTypeImpl")
25-
.or { reflectiveClass("com.android.builder.core.VariantType") }
26-
// Java outputs are accessed through this enum in AGP 3.2.0-alpha02
27-
private static final def InternalArtifactType =
28-
reflectiveClass("com.android.build.gradle.internal.scope.InternalArtifactType")
29-
3020
// No instances
3121
private GroovyInterop() { throw new AssertionError() }
3222

33-
/**
34-
* Attempts to look up a Class based on its FQCN, returns an empty Optional if this fails
35-
* @param fqcn Fully qualified class name
36-
* @return The class, or an empty Optional
37-
*/
38-
private static Optional<Class> reflectiveClass(String fqcn) {
39-
try {
40-
return Optional.of(Class.forName(fqcn))
41-
} catch (ignored) {
42-
return Optional.empty()
43-
}
44-
}
45-
4623
/**
4724
* Obtains the VariantData of the provided Variant.
4825
*
@@ -56,102 +33,42 @@ class GroovyInterop {
5633
}
5734

5835
/**
59-
* Obtains the Java output directory of the provided VariantScope in a safe manner.
60-
* TODO Clean this mess up once the Android Gradle Plugin 3.2.0 finally decides on something. :|
36+
* Obtains the Java artifact files of the provided VariantScope in a safe manner.
6137
*
62-
* @because In Android Gradle Plugin 3.2.0-alpha02, the original method was removed
63-
* @param variant VariantScope to retrieve the Java output directory from
38+
* @because Some scopes, especially for Jacoco, don't have the files at hand through this API
39+
* @param variant VariantScope to retrieve the Java artifact files from
6440
* @return That file
6541
*/
6642
@NonNull
67-
static Set<File> variantScope_javaOutputDirs(VariantScope scope) {
68-
if (scope.hasProperty("buildArtifactsHolder") && InternalArtifactType.isPresent()) {
69-
def artifactType = InternalArtifactType
70-
.map { it.getDeclaredField("JAVAC").get(null) }
71-
.get()
72-
if (scope.buildArtifactsHolder.hasArtifact(artifactType)) {
73-
// 3.2.0-alpha04 and above:
74-
// Java outputs are moved into a subdirectory exposed by the compilation BuildArtifact
75-
return scope.buildArtifactsHolder.getArtifactFiles(artifactType).files
76-
} else {
77-
// 3.2.0-alpha02 and above:
78-
// Java outputs are still inside the "intermediates/classes" directory,
79-
// but there is no public API for that, so construct the path yourself
80-
return [new File(scope.globalScope.intermediatesDir,
81-
"/classes/" + scope.variantConfiguration.dirName)]
82-
}
43+
static Set<File> variantScope_getJavacArtifactFiles(VariantScope scope) {
44+
if (scope.artifacts.hasArtifact(InternalArtifactType.JAVAC)) {
45+
return scope.artifacts.getArtifactFiles(InternalArtifactType.JAVAC).files
8346
} else {
84-
// Below 3.2.0-alpha02:
85-
// Java outputs are expressed through the javaOutputDir property
86-
return [scope.javaOutputDir]
47+
return [new File(scope.globalScope.intermediatesDir,
48+
"/classes/" + scope.variantConfiguration.dirName)]
8749
}
8850
}
8951

90-
/**
91-
* Obtains the Assets Collection of the given AndroidUnitTest.
92-
*
93-
* @because 'assetsCollection' type changed from FileCollection to BuildArtifact in Android Gradle Plugin 3.2.0-alpha07
94-
* @param test The Android JUnit 4 test to access
95-
* @return Its assets collection
96-
*/
97-
@Nullable
98-
static Set<File> androidUnitTest_assetsCollection(AndroidUnitTest test) {
99-
def collection = test.assetsCollection
100-
return collection == null ? null : collection.files
101-
}
102-
103-
/**
104-
* Obtains the Res Collection of the given AndroidUnitTest.
105-
*
106-
* @because 'resCollection' type changed from FileCollection to BuildArtifact in Android Gradle Plugin 3.2.0-alpha11
107-
* @param test The Android JUnit 4 test to access
108-
* @return Its assets collection
109-
*/
110-
@Nullable
111-
static Set<File> androidUnitTest_resCollection(AndroidUnitTest test) {
112-
def collection = test.resCollection
113-
return collection == null ? null : collection.files
114-
}
115-
116-
/**
117-
* Obtains the Merged Manifest of the given AndroidUnitTest.
118-
*
119-
* @because 'mergedManifest' type changed from FileCollection to BuildArtifact in Android Gradle Plugin 3.2.0-alpha07
120-
* @param test The Android JUnit 4 test to access
121-
* @return Its merged manifest
122-
*/
123-
@Nullable
124-
static Set<File> androidUnitTest_mergedManifest(AndroidUnitTest test) {
125-
def collection = test.mergedManifest
126-
return collection == null ? null : collection.files
127-
}
128-
12952
/**
13053
* Obtains the task name prefix for Unit Test variants.
13154
*
132-
* @because In Android Gradle Plugin 3.2.0-alpha06, the underlying constants on VariantType were renamed
133-
* @return The unit test prefix
55+
* @because Kotlin cannot see the VariantTypeImpl class
56+
* @return The unit test task prefix
13457
*/
13558
@NonNull
13659
static String variantType_unitTestPrefix() {
137-
return VariantType
138-
.map { it.getDeclaredField("UNIT_TEST").get(null) }
139-
.map { it.prefix }
140-
.orElseThrow { new IllegalArgumentException("can't get VariantType.UNIT_TEST.prefix") }
60+
return VariantTypeImpl.UNIT_TEST.prefix
14161
}
14262

14363
/**
14464
* Obtains the task name suffix for Unit Test variants.
14565
*
146-
* @because In Android Gradle Plugin 3.2.0-alpha06, the underlying constants on VariantType were renamed
147-
* @return The unit test prefix
66+
* @because Kotlin cannot see the VariantTypeImpl class
67+
* @return The unit test task prefix
14868
*/
14969
@NonNull
15070
static String variantType_unitTestSuffix() {
151-
return VariantType
152-
.map { it.getDeclaredField("UNIT_TEST").get(null) }
153-
.map { it.suffix }
154-
.orElseThrow { new IllegalArgumentException("can't get VariantType.UNIT_TEST.suffix") }
71+
return VariantTypeImpl.UNIT_TEST.suffix
15572
}
15673

15774
/**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package de.mannodermaus.gradle.plugins.junit5
22

33
const val MIN_REQUIRED_GRADLE_VERSION = "4.7"
4-
const val MIN_REQUIRED_AGP_VERSION = "3.2.0-alpha18"
4+
const val MIN_REQUIRED_AGP_VERSION = "3.2.0"
55

66
const val EXTENSION_NAME = "junitPlatform"
77
const val FILTERS_EXTENSION_NAME = "filters"

plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/Interop.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package de.mannodermaus.gradle.plugins.junit5
33
import com.android.build.gradle.api.BaseVariant
44
import com.android.build.gradle.internal.scope.VariantScope
55
import com.android.build.gradle.internal.variant.BaseVariantData
6-
import com.android.build.gradle.tasks.factory.AndroidUnitTest
76
import org.gradle.api.Project
87
import org.gradle.testing.jacoco.tasks.JacocoReportBase
98
import java.io.File
@@ -18,17 +17,8 @@ import java.io.File
1817
val BaseVariant.variantData: BaseVariantData
1918
get() = GroovyInterop.baseVariant_variantData(this)
2019

21-
val VariantScope.safeJavaOutputDirs: Set<File>
22-
get() = GroovyInterop.variantScope_javaOutputDirs(this)
23-
24-
val AndroidUnitTest.safeResCollection: Set<File>?
25-
get() = GroovyInterop.androidUnitTest_resCollection(this)
26-
27-
val AndroidUnitTest.safeAssetsCollection: Set<File>?
28-
get() = GroovyInterop.androidUnitTest_assetsCollection(this)
29-
30-
val AndroidUnitTest.safeMergedManifest: Set<File>?
31-
get() = GroovyInterop.androidUnitTest_mergedManifest(this)
20+
val VariantScope.safeJavacArtifactFiles: Set<File>
21+
get() = GroovyInterop.variantScope_getJavacArtifactFiles(this)
3222

3323
/*
3424
* Compatibility methods for multiple Gradle versions.

plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/providers/Java.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package de.mannodermaus.gradle.plugins.junit5.providers
22

33
import com.android.build.gradle.api.BaseVariant
44
import de.mannodermaus.gradle.plugins.junit5.internal.unitTestVariant
5-
import de.mannodermaus.gradle.plugins.junit5.safeJavaOutputDirs
5+
import de.mannodermaus.gradle.plugins.junit5.safeJavacArtifactFiles
66
import de.mannodermaus.gradle.plugins.junit5.variantData
77

88
/**
@@ -25,5 +25,5 @@ class JavaDirectoryProvider(private val variant: BaseVariant) : DirectoryProvide
2525
.toSet()
2626

2727
private fun classFoldersOf(variant: BaseVariant) =
28-
variant.variantData.scope.safeJavaOutputDirs
28+
variant.variantData.scope.safeJavacArtifactFiles
2929
}

plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/PluginSpec.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,7 @@ class PluginSpec : Spek({
338338
// create each class file in multiple directories to remain compatible with all approaches
339339
// TODO Clean this mess up once the Android Gradle Plugin 3.2.0 finally decides on something. :|
340340
listOf(
341-
// AGP 3.2.0-alpha07 and above
342341
"build/intermediates/javac/debug/compileDebugJavaWithJavac/classes",
343-
// AGP 3.2.0-alpha06
344-
"build/intermediates/artifact_transform/compileDebugJavaWithJavac/classes",
345-
// AGP 3.2.0-alpha04 and above
346-
"build/intermediates/artifact_transform/javac/debug/classes",
347-
// Everything below
348342
"build/intermediates/classes/debug").forEach { folder ->
349343
project.file(folder).mkdirs()
350344
project.file("$folder/R.class").createNewFile()
@@ -353,13 +347,7 @@ class PluginSpec : Spek({
353347
}
354348

355349
listOf(
356-
// AGP 3.2.0-alpha07 and above
357350
"build/intermediates/javac/release/compileReleaseJavaWithJavac/classes",
358-
// AGP 3.2.0-alpha06
359-
"build/intermediates/artifact_transform/compileReleaseJavaWithJavac/classes",
360-
// AGP 3.2.0-alpha04 and above
361-
"build/intermediates/artifact_transform/javac/release/classes",
362-
// Everything below
363351
"build/intermediates/classes/release").forEach { folder ->
364352
project.file(folder).mkdirs()
365353
project.file("$folder/R.class").createNewFile()

plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/VersionCheckerSpec.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ class VersionCheckerSpec : Spek({
2020
"3.2.0-alpha01" to false,
2121
"3.2.0-alpha14" to false,
2222
MIN_REQUIRED_AGP_VERSION to true,
23-
"3.2.0-beta01" to true,
24-
"3.2.0" to true,
23+
"3.3.0" to true,
24+
"3.4.0" to true,
25+
"3.5.0-alpha13" to true,
2526
"3.3.0" to true,
2627
"4.0.0-alpha01" to true
2728
).forEach { (actual, expected) ->

plugin/android-junit5/src/test/projects/agp32x/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ plugins {
99
id "com.android.application"
1010
id "org.jetbrains.kotlin.android"
1111
id "de.mannodermaus.android-junit5"
12+
id "jacoco"
1213
}
1314

1415
def version = com.android.builder.model.Version.ANDROID_GRADLE_PLUGIN_VERSION

0 commit comments

Comments
 (0)