Skip to content

Commit 67e5ad6

Browse files
authored
Using new configureJavaCompileTask in new variant api (#1894)
AGP 9.0.0-alpha06 introduces the new `configureJavaCompileTask` API, which provides a dedicated configuration hook for the Java compile task. This allows us to apply our workaround directly during task configuration. This only applies to Application and Library variants (`onVariants`), and not to unit test or Android test variants (`beforeVariants`). We’ll create a new issue to follow up with the Android team.
1 parent 804b168 commit 67e5ad6

File tree

5 files changed

+38
-17
lines changed

5 files changed

+38
-17
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ org.gradle.kotlin.dsl.allWarningsAsErrors=true
66

77
systemProp.pts.enabled=true
88

9-
org.gradle.android.latestKnownAgpVersion=9.0.0-alpha04
9+
org.gradle.android.latestKnownAgpVersion=9.0.0-alpha06

src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.gradle.android.workarounds
33
import com.google.common.annotations.VisibleForTesting
44
import com.google.common.collect.Lists
55
import org.gradle.android.AndroidIssue
6+
import org.gradle.android.VersionNumber
67
import org.gradle.android.Versions
78
import org.gradle.api.Project
89
import org.gradle.api.artifacts.transform.CacheableTransform
@@ -60,20 +61,30 @@ class JdkImageWorkaround implements Workaround {
6061
// runtime configuration before querying (and instantiating) task configurations.
6162
applyRuntimeClasspathNormalization(project)
6263

63-
applyToAllAndroidVariants(project) { variant ->
64-
variant.javaCompileProvider.configure { JavaCompile task ->
65-
def jdkImageInput = getJdkImageInput(task)
66-
if (jdkImageInput != null) {
67-
setupExtractedJdkImageInputTransform(project, getJvmHome(task))
68-
replaceCommandLineProvider(task, jdkImageInput)
64+
if (Versions.CURRENT_ANDROID_VERSION < VersionNumber.parse("9.0.0-alpha04")) {
65+
applyToAllAndroidVariantsLegacy(project) { variant ->
66+
variant.javaCompileProvider.configure { JavaCompile task ->
67+
jdkTransform(project, task)
68+
}
69+
}
70+
} else {
71+
applyToAllAndroidVariants(project) { variant ->
72+
variant.configureJavaCompileTask { compileTask ->
73+
jdkTransform(project, compileTask)
6974
}
7075
}
7176
}
7277
}
7378

74-
// Configuration for Old Variant API will drop in AGP 9. We will need to use a different
75-
// approach to retrieve the variants using the new Variant API.
76-
private static void applyToAllAndroidVariants(Project project, Closure<?> configureVariant) {
79+
private static void jdkTransform(Project project, JavaCompile task) {
80+
def jdkImageInput = getJdkImageInput(task)
81+
if (jdkImageInput != null) {
82+
setupExtractedJdkImageInputTransform(project, getJvmHome(task))
83+
replaceCommandLineProvider(task, jdkImageInput)
84+
}
85+
}
86+
87+
private static void applyToAllAndroidVariantsLegacy(Project project, Closure<?> configureVariant) {
7788
project.plugins.withId("com.android.application") {
7889
def android = project.extensions.findByName("android")
7990
android.unitTestVariants.all(configureVariant)
@@ -87,6 +98,20 @@ class JdkImageWorkaround implements Workaround {
8798
}
8899
}
89100

101+
private static void applyToAllAndroidVariants(Project project, Closure<?> configureVariant) {
102+
project.plugins.withId("com.android.application") {
103+
def androidComponents = project.extensions.findByName("androidComponents")
104+
def selector = androidComponents.selector()
105+
androidComponents.onVariants(selector.all(), configureVariant)
106+
}
107+
108+
project.plugins.withId("com.android.library") {
109+
def androidComponents = project.extensions.findByName("androidComponents")
110+
def selector = androidComponents.selector()
111+
androidComponents.onVariants(selector.all(), configureVariant)
112+
}
113+
}
114+
90115
static def applyRuntimeClasspathNormalization(Project project) {
91116
project.normalization { handler ->
92117
handler.runtimeClasspath {

src/test/groovy/org/gradle/android/SimpleAndroidApp.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ class SimpleAndroidApp {
117117

118118
file("gradle.properties") << """
119119
android.useAndroidX=true
120-
android.newDsl=false
121120
org.gradle.jvmargs=-Xmx1536m -Dkotlin.daemon.jvm.options=-Xmx768m,-Xms256m
122121
kapt.use.worker.api=${kaptWorkersEnabled}
123122
android.experimental.enableSourceSetPathsMap=true

src/test/resources/expectedOutcomes/9.0_outcomes.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@
3434
":app:extractReleaseVersionControlInfo" : "SUCCESS",
3535
":app:generateDebugAssets" : "UP_TO_DATE",
3636
":app:generateDebugResources" : "FROM_CACHE",
37-
":app:generateDebugResValues" : "FROM_CACHE",
3837
":app:generateReleaseAssets" : "UP_TO_DATE",
3938
":app:generateReleaseResources" : "FROM_CACHE",
40-
":app:generateReleaseResValues" : "FROM_CACHE",
4139
":app:javaPreCompileDebug" : "FROM_CACHE",
4240
":app:javaPreCompileRelease" : "FROM_CACHE",
4341
":app:mapDebugSourceSetPaths" : "SUCCESS",
@@ -128,11 +126,9 @@
128126
":library:extractReleaseAnnotations" : "FROM_CACHE",
129127
":library:generateDebugAssets" : "UP_TO_DATE",
130128
":library:generateDebugResources" : "FROM_CACHE",
131-
":library:generateDebugResValues" : "FROM_CACHE",
132129
":library:generateDebugRFile" : "FROM_CACHE",
133130
":library:generateReleaseAssets" : "UP_TO_DATE",
134131
":library:generateReleaseResources" : "FROM_CACHE",
135-
":library:generateReleaseResValues" : "FROM_CACHE",
136132
":library:generateReleaseRFile" : "FROM_CACHE",
137133
":library:javaPreCompileDebug" : "FROM_CACHE",
138134
":library:javaPreCompileRelease" : "FROM_CACHE",
@@ -180,5 +176,6 @@
180176
":app:processReleaseNavigationResources" : "FROM_CACHE",
181177
":app:compileReleaseNavigationResources" : "FROM_CACHE",
182178
":app:generateDebugRFile" : "FROM_CACHE",
183-
":app:generateReleaseRFile" : "FROM_CACHE"
179+
":app:generateReleaseRFile" : "FROM_CACHE",
180+
":library:compileReleaseNavigationResources" : "FROM_CACHE"
184181
}

src/test/resources/versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"testedVersions": {
3-
"9.0.0-alpha04": [
3+
"9.0.0-alpha06": [
44
"9.0.0"
55
],
66
"8.13.0": [

0 commit comments

Comments
 (0)