[Bug]: samples won't work in Android Studio Flamingo because `kotlinOptions.jvmTarget` isn't set
Is there an existing issue for this?
- [X] I have searched the existing issues
Is there a StackOverflow question about this issue?
- [X] I have searched StackOverflow
Is this an issue related to one of the samples?
- [X] Yes, this is a specific issue related to this samples repo.
Sample app
JetNews
What happened?
When upgrading any sample to kotlin 1.8.0 or AGP 8.0.0:
Execution failed for task ':app:compileDebugKotlin'.
> 'compileDebugJavaWithJavac' task (current target is 1.8) and 'compileDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.
Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain
This could be solved by adding this to app/build.gradle.kts:
kotlinOptions {
jvmTarget = '1.8'
}
Or by changing compileOptions.targetCompatibility to JavaVersion.VERSION_17
Relevant logcat output
No response
Code of Conduct
- [X] I agree to follow this project's Code of Conduct
CC https://github.com/android/compose-samples/pull/1114
'compileProdReleaseJavaWithJavac' task (current target is 1.8) and 'kaptGenerateStubsProdReleaseKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.
Nothing is working for me.
kotlin{ IjvmToolchain(18) }
above code did not help
You just need to have the same version of kotlin JDK and Java target JDK. The exact fix may vary depending on which gradle task you are changing. Here is an example https://github.com/android/compose-samples/pull/1110
I tried all the way nothing is working
Caused by: org.gradle.api.GradleException: 'compileProdReleaseJavaWithJavac' task (current target is 1.8) and 'kaptGenerateStubsProdReleaseKotlin' task (current target is 18) jvm target compatibility should be set to the same Java version.
Could you share where and what is to be change?
this code is already written in gradle
compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' }
I tried all the way nothing is working
Caused by: org.gradle.api.GradleException: 'compileProdReleaseJavaWithJavac' task (current target is 1.8) and 'kaptGenerateStubsProdReleaseKotlin' task (current target is 18) jvm target compatibility should be set to the same Java version.
Could you share where and what is to be change?
this code is already written in gradle
compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' }
I have the same configuration in most of my modules and I still get this error after upgrading to Flamingo. What are we supposed to do?
Following code resolved the issue
compileOptions { sourceCompatibility JavaVersion.VERSION_18 targetCompatibility JavaVersion.VERSION_18 } kotlinOptions { jvmTarget = '18' }
https://github.com/android/compose-samples/pull/1114 has been merged, could you pull from latest main and confirm you're able to run samples in flamingo? TY
Removing
kotlinOptions {
jvmTarget = '1.8'
}
And setting
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
Worked for me! Had this issue straight after updating to Android Studio Flamingo.
Is it safe to just target version 17 and 18 in a production app? Does it depend on what Android API you are targeting? The app I work on targets JDK 1.8 because it's what Hilt's documentation recommends. I would really like to understand what the proper solution for this kind of issue is. According to this thread there has been a change in kapt that no longer picks up the jvmTarget setting. Therefore, the workaround I went with is:
allprojects {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
kotlinOptions.jvmTarget = "1.8"
}
}
I am just not confident changing the source and target compatibility settings or making use of Gradle's JVM toolchain are the right solutions.
I advise everyone using samples to take the latest version for main, since it sets JavaVersion.VERSION_17. Otherwise you can set it yourself for a project and remove kotlinOptions jvmTarget, because it uses VERSION_17 by default with the latest AGP 8.0.