@@ -2,47 +2,24 @@ package de.mannodermaus.gradle.plugins.junit5
2
2
3
3
import com.android.annotations.NonNull
4
4
import com.android.build.gradle.api.BaseVariant
5
+ import com.android.build.gradle.internal.scope.InternalArtifactType
5
6
import com.android.build.gradle.internal.scope.VariantScope
6
7
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
9
9
import org.gradle.api.Project
10
10
import org.gradle.api.file.ConfigurableFileCollection
11
11
import org.gradle.testing.jacoco.tasks.JacocoReportBase
12
12
13
- import javax.annotation.Nullable
14
-
15
13
/**
16
14
* Utility functions exposed to Kotlin consumers
17
15
* that can't safely access Groovy members otherwise,
18
16
* or require reflection to access in a compatible manner
19
17
* across all supported versions of the Android Gradle Plugin.*/
20
18
class GroovyInterop {
21
19
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
-
30
20
// No instances
31
21
private GroovyInterop () { throw new AssertionError () }
32
22
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
-
46
23
/**
47
24
* Obtains the VariantData of the provided Variant.
48
25
*
@@ -56,102 +33,42 @@ class GroovyInterop {
56
33
}
57
34
58
35
/**
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.
61
37
*
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
64
40
* @return That file
65
41
*/
66
42
@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
83
46
} 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)]
87
49
}
88
50
}
89
51
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
-
129
52
/**
130
53
* Obtains the task name prefix for Unit Test variants.
131
54
*
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
134
57
*/
135
58
@NonNull
136
59
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
141
61
}
142
62
143
63
/**
144
64
* Obtains the task name suffix for Unit Test variants.
145
65
*
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
148
68
*/
149
69
@NonNull
150
70
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
155
72
}
156
73
157
74
/**
0 commit comments