juliocbcotta
04/03/2023, 3:09 PMandroid {
compileOptions {
sourceCompatibility JavaVersion.VERSION_..
targetCompatibility JavaVersion.VERSION_..
}
kotlinOptions {
jvmTarget = 'X.Y'
}
}
kotlin {
jvmToolchain {
languageVersion = JavaLanguageVersion.of(XX)
}
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(YY)
}
}
For instance, my project has minSdk 24 and I would like to compile it using JDK 17 to have better GC while compiling it.
If I set languageVersion = JavaLanguageVersion.of(17)
with sourceCompatibility
, targetCompatibility
and jvmTarget
to Java 1.8, I get an error in kapt generated classes... googling it I found this config
kapt {
javacOptions {
option("--source", "8")
option("--target", "8")
}
}
Now I get this warning:
'compileDebugJavaWithJavac' task (current target is 1.8) and 'kaptGenerateStubsDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.
AGP 7.4.1, Kotlin 1.8.10
How to fix that warning ? Can I set my sourceCompatibility
, targetCompatibility
and jvmTarget
to Java 11 or 17 without breaking the app in old android versions?dave08
04/03/2023, 3:19 PMandroid { }
block:
kotlin {
jvmToolchain(11)
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
and in AS settings for Gradle select 17... (assuming you're using AGP > 8dave08
04/03/2023, 3:20 PMdave08
04/03/2023, 3:20 PMdave08
04/03/2023, 3:20 PMjuliocbcotta
04/03/2023, 3:30 PMjuliocbcotta
04/03/2023, 3:30 PMdave08
04/03/2023, 3:31 PMdave08
04/03/2023, 3:32 PMjuliocbcotta
04/03/2023, 3:33 PMdave08
04/03/2023, 3:34 PMjuliocbcotta
04/03/2023, 3:35 PM'compileDebugJavaWithJavac' task (current target is 11) and 'kaptGenerateStubsDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.
I changed everything that was pointing to 1.8 to 11juliocbcotta
04/03/2023, 3:35 PMlanguageVersion = JavaLanguageVersion.of(17)
juliocbcotta
04/03/2023, 3:35 PMMartin Sloup
04/05/2023, 12:22 PM// use JDK 11 for compilation
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}
android {
...
compileOptions {
// Flag to enable support for the new language APIs
// <https://developer.android.com/studio/write/java8-support#library-desugaring>
coreLibraryDesugaringEnabled true
// Android generate JDK 11 classes
sourceCompatibility = "11"
targetCompatibility = "11"
}
kotlinOptions {
// Kotlin generate JDK 11 classes
jvmTarget = "11"
}
}
dependencies {
// see versions: <https://developer.android.com/studio/write/java8-support#library-desugaring-versions>
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2'
}
I use Gradle 8.0.2, but this configuration is also supported with latest Gradle 7.x versionjuliocbcotta
04/05/2023, 3:32 PMjuliocbcotta
04/05/2023, 3:32 PMEugen Martynov
04/11/2023, 1:43 PMEugen Martynov
04/11/2023, 1:43 PMEugen Martynov
04/11/2023, 1:43 PMMartin Sloup
04/17/2023, 2:55 PMjava.toolchain.languageVersion
• Android and Java generate code / bytecode by android.compileOptions.*
configuration
• Kotlin generate code / bytecode by android.kotlin.jvmToolchain
configuration on Kotlin 1.7.20+, see here. By default it use version from java.toolchain.languageVersion
. I am not sure if it must be in android block, or not, but it works in android block.
• Kotlin compiler complains if JDK version for Kotlin and Java is different