From 836c41ce9b1d4d3a9c9f67c22ff118f2166b15f1 Mon Sep 17 00:00:00 2001 From: Inaki Villar Date: Tue, 6 Feb 2024 14:22:37 -0800 Subject: [PATCH 1/4] only using ModuleDescriptor for AGP < 8 --- .../workarounds/JdkImageWorkaround.groovy | 20 +++--- .../android/JdkImageWorkaroundTest.groovy | 67 +++++++++++++++++++ 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy b/src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy index 4470956a..e4e42c57 100644 --- a/src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy +++ b/src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy @@ -3,6 +3,7 @@ package org.gradle.android.workarounds import com.google.common.annotations.VisibleForTesting import com.google.common.collect.Lists import org.gradle.android.AndroidIssue +import org.gradle.android.Versions import org.gradle.api.Project import org.gradle.api.artifacts.transform.CacheableTransform import org.gradle.api.artifacts.transform.InputArtifact @@ -188,14 +189,17 @@ class JdkImageWorkaround implements Workaround { ) } - // Capture the module descriptor ignoring the version, which is not enforced anyways - File moduleInfoFile = new File(targetDir, 'java.base/module-info.class') - ModuleDescriptor descriptor = captureModuleDescriptorWithoutVersion(moduleInfoFile) - File descriptorData = new File(targetDir, "module-descriptor.txt") - descriptorData.text = serializeDescriptor(descriptor) - - fileOperations.delete { - delete(moduleInfoFile) + // Since AGP 8 only major Java version is stored in the output, we don't need to use the ModuleDescriptor + // to create a new descriptor file + if (Versions.CURRENT_ANDROID_VERSION.major < 8) { + // Capture the module descriptor ignoring the version, which is not enforced anyways + File moduleInfoFile = new File(targetDir, 'java.base/module-info.class') + ModuleDescriptor descriptor = captureModuleDescriptorWithoutVersion(moduleInfoFile) + File descriptorData = new File(targetDir, "module-descriptor.txt") + descriptorData.text = serializeDescriptor(descriptor) + fileOperations.delete { + delete(moduleInfoFile) + } } } diff --git a/src/test/groovy/org/gradle/android/JdkImageWorkaroundTest.groovy b/src/test/groovy/org/gradle/android/JdkImageWorkaroundTest.groovy index af866c58..3c8f8072 100644 --- a/src/test/groovy/org/gradle/android/JdkImageWorkaroundTest.groovy +++ b/src/test/groovy/org/gradle/android/JdkImageWorkaroundTest.groovy @@ -257,4 +257,71 @@ class JdkImageWorkaroundTest extends AbstractTest { buildResult.task(':library:compileDebugJavaWithJavac').outcome == TaskOutcome.FROM_CACHE buildResult.task(':library:compileReleaseJavaWithJavac').outcome == TaskOutcome.FROM_CACHE } + + def "jdkImage is normalized when using different JDK version in the build and toolchain configuration"() { + + def zuluPath = System.getProperty(ZULU_PATH) + Assume.assumeTrue("Zulu path is not available", zuluPath != null && new File(zuluPath).exists()) + Assume.assumeTrue("Android Gradle Plugin < 8", androidVersion >= VersionNumber.parse("8.0")) + + + def gradleVersion = TestVersions.latestSupportedGradleVersionFor(androidVersion) + SimpleAndroidApp.builder(temporaryFolder.root, cacheDir) + .withAndroidVersion(androidVersion) + .withKotlinDisabled() + .withToolchainVersion("19") + .withSourceCompatibility(JavaVersion.VERSION_1_9) + .withDatabindingDisabled() + .build() + .writeProject() + + when: + BuildResult buildResult = withGradleVersion(gradleVersion.version) + .withProjectDir(temporaryFolder.root) + .withEnvironment( + System.getenv() + + ["JDK": zuluPath] + ) + .withArguments( + "clean", "testDebug", "testRelease", "assemble", + "--build-cache", + "-Porg.gradle.java.installations.auto-detect=false", + "-Porg.gradle.java.installations.fromEnv=JDK" + ).build() + + then: + buildResult.task(':app:compileDebugJavaWithJavac').outcome == TaskOutcome.SUCCESS + buildResult.task(':library:compileDebugJavaWithJavac').outcome == TaskOutcome.SUCCESS + + buildResult.task(':app:compileDebugUnitTestJavaWithJavac').outcome == TaskOutcome.SUCCESS + buildResult.task(':library:compileDebugUnitTestJavaWithJavac').outcome == TaskOutcome.SUCCESS + + when: + buildResult = withGradleVersion(gradleVersion.version) + .withProjectDir(temporaryFolder.root) + .withEnvironment( + System.getenv() + + ["JDK": zuluPath] + ) + .withArguments( + "clean", "testDebug", "testRelease", "assemble", + "--build-cache", + "-Porg.gradle.java.installations.auto-detect=false", + "-Porg.gradle.java.installations.fromEnv=JDK" + ).build() + + then: + buildResult.task(':app:compileDebugJavaWithJavac').outcome == TaskOutcome.FROM_CACHE + buildResult.task(':app:compileReleaseJavaWithJavac').outcome == TaskOutcome.FROM_CACHE + buildResult.task(':library:compileDebugJavaWithJavac').outcome == TaskOutcome.FROM_CACHE + buildResult.task(':library:compileReleaseJavaWithJavac').outcome == TaskOutcome.FROM_CACHE + + buildResult.task(':app:compileDebugUnitTestJavaWithJavac').outcome == TaskOutcome.FROM_CACHE + buildResult.task(':app:compileReleaseUnitTestJavaWithJavac').outcome == TaskOutcome.FROM_CACHE + buildResult.task(':library:compileDebugUnitTestJavaWithJavac').outcome == TaskOutcome.FROM_CACHE + buildResult.task(':library:compileReleaseUnitTestJavaWithJavac').outcome == TaskOutcome.FROM_CACHE + + where: + androidVersion << TestVersions.latestAndroidVersions + } } From f24f86dc0dd49186ab390db70f2ce00e340ec8b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Villar?= Date: Thu, 8 Feb 2024 15:07:17 -0800 Subject: [PATCH 2/4] Update src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy Co-authored-by: Nelson Osacky --- .../org/gradle/android/workarounds/JdkImageWorkaround.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy b/src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy index e4e42c57..c95928d6 100644 --- a/src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy +++ b/src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy @@ -189,7 +189,7 @@ class JdkImageWorkaround implements Workaround { ) } - // Since AGP 8 only major Java version is stored in the output, we don't need to use the ModuleDescriptor + // Starting with AGP 8 only the major Java version is stored in the output so we don't need any normalization // to create a new descriptor file if (Versions.CURRENT_ANDROID_VERSION.major < 8) { // Capture the module descriptor ignoring the version, which is not enforced anyways From c6b12c14219bb80731df59f58e1e39235a7ff4b3 Mon Sep 17 00:00:00 2001 From: Inaki Villar Date: Thu, 8 Feb 2024 19:04:14 -0800 Subject: [PATCH 3/4] removing the zulu path --- .../android/JdkImageWorkaroundTest.groovy | 20 +++---------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/test/groovy/org/gradle/android/JdkImageWorkaroundTest.groovy b/src/test/groovy/org/gradle/android/JdkImageWorkaroundTest.groovy index 3c8f8072..27a40a39 100644 --- a/src/test/groovy/org/gradle/android/JdkImageWorkaroundTest.groovy +++ b/src/test/groovy/org/gradle/android/JdkImageWorkaroundTest.groovy @@ -258,10 +258,8 @@ class JdkImageWorkaroundTest extends AbstractTest { buildResult.task(':library:compileReleaseJavaWithJavac').outcome == TaskOutcome.FROM_CACHE } - def "jdkImage is normalized when using different JDK version in the build and toolchain configuration"() { + def "jdkImage is normalized when using different toolchain configuration"() { - def zuluPath = System.getProperty(ZULU_PATH) - Assume.assumeTrue("Zulu path is not available", zuluPath != null && new File(zuluPath).exists()) Assume.assumeTrue("Android Gradle Plugin < 8", androidVersion >= VersionNumber.parse("8.0")) @@ -278,15 +276,9 @@ class JdkImageWorkaroundTest extends AbstractTest { when: BuildResult buildResult = withGradleVersion(gradleVersion.version) .withProjectDir(temporaryFolder.root) - .withEnvironment( - System.getenv() + - ["JDK": zuluPath] - ) .withArguments( "clean", "testDebug", "testRelease", "assemble", - "--build-cache", - "-Porg.gradle.java.installations.auto-detect=false", - "-Porg.gradle.java.installations.fromEnv=JDK" + "--build-cache" ).build() then: @@ -299,15 +291,9 @@ class JdkImageWorkaroundTest extends AbstractTest { when: buildResult = withGradleVersion(gradleVersion.version) .withProjectDir(temporaryFolder.root) - .withEnvironment( - System.getenv() + - ["JDK": zuluPath] - ) .withArguments( "clean", "testDebug", "testRelease", "assemble", - "--build-cache", - "-Porg.gradle.java.installations.auto-detect=false", - "-Porg.gradle.java.installations.fromEnv=JDK" + "--build-cache" ).build() then: From 869ca6101514ae8a513af495812ab56fec9f7fde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Villar?= Date: Fri, 9 Feb 2024 07:22:35 -0800 Subject: [PATCH 4/4] Update src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy Co-authored-by: Nelson Osacky --- .../org/gradle/android/workarounds/JdkImageWorkaround.groovy | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy b/src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy index c95928d6..65c68291 100644 --- a/src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy +++ b/src/main/groovy/org/gradle/android/workarounds/JdkImageWorkaround.groovy @@ -190,7 +190,6 @@ class JdkImageWorkaround implements Workaround { } // Starting with AGP 8 only the major Java version is stored in the output so we don't need any normalization - // to create a new descriptor file if (Versions.CURRENT_ANDROID_VERSION.major < 8) { // Capture the module descriptor ignoring the version, which is not enforced anyways File moduleInfoFile = new File(targetDir, 'java.base/module-info.class')