compose-samples icon indicating copy to clipboard operation
compose-samples copied to clipboard

[Bug]: samples won't work in Android Studio Flamingo because `kotlinOptions.jvmTarget` isn't set

Open obask opened this issue 3 years ago • 11 comments

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

obask avatar Apr 06 '23 23:04 obask

CC https://github.com/android/compose-samples/pull/1114

astamato avatar Apr 14 '23 09:04 astamato

'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.

noufil avatar Apr 15 '23 12:04 noufil

kotlin{ IjvmToolchain(18) }

above code did not help

noufil avatar Apr 15 '23 12:04 noufil

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

obask avatar Apr 15 '23 17:04 obask

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' }

noufil avatar Apr 15 '23 20:04 noufil

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?

argenkiwi avatar Apr 16 '23 22:04 argenkiwi

Following code resolved the issue

compileOptions { sourceCompatibility JavaVersion.VERSION_18 targetCompatibility JavaVersion.VERSION_18 } kotlinOptions { jvmTarget = '18' }

noufil avatar Apr 17 '23 07:04 noufil

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

astamato avatar Apr 17 '23 10:04 astamato

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.

fedestyla avatar Apr 17 '23 15:04 fedestyla

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.

argenkiwi avatar Apr 17 '23 20:04 argenkiwi

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.

obask avatar Apr 17 '23 21:04 obask