Jan Skrasek
02/28/2025, 9:10 AMorg.gradle.toolchains.foojay-resolver-convention
combined with toolchain setup will make it work automatically. (i.e. will use proper JDK without manual reconfiguration of default JDK/IntelliJ)
I want the build:
• use JDK 21 to build
• want Java 11 jvmTarget to be produced
• currently, to test, JDK 18 is the default in the system/in Android Studio.
Setup:
extensions.getByType<JavaPluginExtension>().apply {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
}
}
tasks.withType<KotlinCompile>().configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_11)
}
}
commonExtension.compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
isCoreLibraryDesugaringEnabled = true
}
The build produces this error for hilt task:
Compilation failed; see the compiler output below.
error: cannot access module-info
bad class file: /modules/java.base/module-info.class
class file has wrong version 65.0, should be 62.0
Any idea what Im doing wrong?Vampire
02/28/2025, 9:46 AM--scan
URL with the error, or at lest a full --stacktrace
if not?Vampire
02/28/2025, 9:46 AMmodule-info.class
of java.base
to be from Java 18 while it is from Java 21Jan Skrasek
02/28/2025, 10:35 AMJan Skrasek
02/28/2025, 10:35 AMJan Skrasek
02/28/2025, 10:37 AMVampire
02/28/2025, 11:17 AM./gradlew :app:hiltJavaCompileDevHsiDebug --debug
you should have in the output NormalizingJavaCompiler
logging the Compiler arguments
, what does that log line look like?Vampire
02/28/2025, 11:17 AMJan Skrasek
02/28/2025, 11:20 AMVampire
02/28/2025, 11:33 AMJan Skrasek
02/28/2025, 11:33 AMCopy code2025-02-28T12:24:33.266+0100 [DEBUG] [org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler] Compiler arguments: -source 11 -target 11
Vampire
02/28/2025, 11:34 AMJan Skrasek
02/28/2025, 11:34 AMJan Skrasek
02/28/2025, 11:37 AM/Users/hrach/storage/__redacted__/lr/app/build/tmp/kotlin-classes/devHsiDebug:/Users/hrach/storage/__redacted__/lr/app/build/tmp/kapt3/classes/devHsiDebug:/Users/hrach/storage/__redacted__/lr/app/build/intermediates/javac/devHsiDebug/compileDevHsiDebugJavaWithJavac/classes -XDstringConcat=inline -parameters -Adagger.fastInit=enabled -Adagger.hilt.internal.useAggregatingRootProcessor=false -Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true -Adagger.fastInit=enabled -Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true -Adagger.hilt.android.internal.projectType=APP -Adagger.hilt.internal.useAggregatingRootProcessor=false --system /Users/hrach/.gradle/caches/8.13/transforms/0f023d2f24aaa91a6672c9a6b20b530c/transformed/output/jdkImage /Users/hrach/storage/__redacted__/lr/app/build/generated/hilt/component_trees/devHsiDebug/dagger/hilt/internal/processedrootsentinel/codegen/_com___redacted___App.java /Users/hrach/storage/__redacted__/lr/app/build/generated/hilt/component_trees/devHsiDebug/com/__redacted__/App_ComponentTreeDeps.java
I see stuff from app module, also the kapt3 path.Vampire
02/28/2025, 12:02 PMVampire
02/28/2025, 12:23 PM/Users/hrach/.gradle/caches/8.13/transforms/0f023d2f24aaa91a6672c9a6b20b530c/transformed/output/jdkImage
contains a Java 18 JDK and something does not like that.
Not sure where this is coming from, maybe some Android-specific thing, Android is always a bit special. 😄Jan Skrasek
02/28/2025, 12:53 PMrelease
file there contains
JAVA_VERSION="21"
MODULES="java.base"
Is that inline with that?Vampire
02/28/2025, 1:00 PMJan Skrasek
02/28/2025, 1:05 PM2025-02-28T12:24:33.257+0100 [DEBUG] [org.gradle.cache.internal.DefaultCacheCoordinator] Creating new cache for jarAnalysis, path /Users/hrach/.gradle/caches/8.13/javaCompile/jarAnalysis.bin, access org.gradle.cache.internal.DefaultCacheCoordinator@1cfc1cc4
2025-02-28T12:24:33.257+0100 [DEBUG] [org.gradle.cache.internal.DefaultInMemoryCacheDecoratorFactory] Creating in-memory store for cache /Users/hrach/.gradle/caches/8.13/javaCompile/jarAnalysis.bin (max size: 103800)
2025-02-28T12:24:33.257+0100 [DEBUG] [org.gradle.cache.internal.LockOnDemandCrossProcessCacheAccess] Acquiring file lock for Java compile cache (/Users/hrach/.gradle/caches/8.13/javaCompile)
2025-02-28T12:24:33.257+0100 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Waiting to acquire exclusive lock on Java compile cache (/Users/hrach/.gradle/caches/8.13/javaCompile).
2025-02-28T12:24:33.257+0100 [DEBUG] [org.gradle.cache.internal.DefaultFileLockManager] Lock acquired on Java compile cache (/Users/hrach/.gradle/caches/8.13/javaCompile).
2025-02-28T12:24:33.258+0100 [DEBUG] [org.gradle.cache.internal.DefaultCacheCoordinator] Creating new cache for classAnalysis, path /Users/hrach/.gradle/caches/8.13/javaCompile/classAnalysis.bin, access org.gradle.cache.internal.DefaultCacheCoordinator@1cfc1cc4
2025-02-28T12:24:33.258+0100 [DEBUG] [org.gradle.cache.internal.DefaultInMemoryCacheDecoratorFactory] Creating in-memory store for cache /Users/hrach/.gradle/caches/8.13/javaCompile/classAnalysis.bin (max size: 2076800)
2025-02-28T12:24:33.261+0100 [INFO] [org.gradle.api.internal.tasks.compile.incremental.SelectiveCompiler] Full recompilation is required because no incremental change information is available. This is usually caused by clean builds or changing compiler arguments.
2025-02-28T12:24:33.262+0100 [INFO] [org.gradle.jvm.toolchain.internal.DefaultToolchainJavaCompiler] Compiling with toolchain '/Users/hrach/Library/Java/JavaVirtualMachines/azul-18.0.2.1/Contents/Home'.
But no 18 anywhere, just the default JDK in the AndroidStudio/JAVA_HOME.Vampire
02/28/2025, 1:10 PMVampire
02/28/2025, 1:10 PMVampire
02/28/2025, 1:10 PMhiltJavaCompileDevHsiDebug
Vampire
02/28/2025, 1:11 PMVampire
02/28/2025, 1:11 PMJan Skrasek
02/28/2025, 1:12 PMJan Skrasek
02/28/2025, 1:12 PMVampire
02/28/2025, 1:13 PMval hiltJavaCompileDevHsiDebug by tasks.existing(JavaCompile::class) {
javaCompiler = javaToolchains.compilerFor(the<JavaPluginExtension>().toolchain)
}
Vampire
02/28/2025, 1:13 PMVampire
02/28/2025, 1:14 PMextensions.getByType<JavaPluginExtension>().apply {
is better written as configure<JavaPluginExtension> {
.
Works the same but is nicer to read and write.Vampire
02/28/2025, 1:17 PMJavaCompile
tasks use the configured toolchain by default: https://github.com/gradle/gradle/issues/16791Vampire
02/28/2025, 1:18 PMJavaExec
, but probably in the same veinJan Skrasek
02/28/2025, 1:22 PMafterEvaluate {
val hiltJavaCompileDevHsiDebug by tasks.existing(JavaCompile::class) {
javaCompiler = javaToolchains.compilerFor(java.toolchain)
}
}
Thank you very much!Vampire
02/28/2025, 1:23 PMafterEvaluate
!
If the task is only registered later, use a lazy construct like
tasks.withType<JavaCompile>().named { it == "hiltJavaCompileDevHsiDebug" }.configureEach {
javaCompiler = javaToolchains.compilerFor(java.toolchain)
}
Jan Skrasek
02/28/2025, 1:24 PMJan Skrasek
02/28/2025, 2:01 PM