How to easily resolve `Execution failed for task ‘:app:kspDebugKotlin” in Android app

When trying to use the Room database in an Android app, execution of the Android app failed due to a Java version mismatch related to ksp. I present an easy way to solve this problem without failure. (Korean version of this article: https://hhtt.kr/102886)

Java version mismatch error occurred regarding Room and ksp

When I was practcing the use of the Room database in my Android app, an error occurred while running the app, and the following was output in Android Studio:

Execution failed for task ':app:kspDebugKotlin'.
 'compileDebugJavaWithJavac' task (current target is 1.8) and 'kspDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.

Before including Room and ksp, it ran without problems. I thought it would be a simple solution, but searching didn’t help at all and I wasted several hours last night and ended up falling asleep without solving it.

I had tried all the solutions suggested by many people, but they didn’t work.

While I was sleeping, a good idea occurred to me.

I felt bad about going to sleep without being able to solve the problem. While I was sleeping and at some point, I realized.

‘The Google codelab app explaining the use of the Room database ran without any problems. Okay, if I compare its settings with my ones, the problem will be resolved quickly. I think Google’s excellent developers must have done a good job.’

With the correct answer next to me, I was having a hard time.

Comparing my project with google codelab project

First, I opened my project and the Google codelab project one by one in Android Studio (each in its respective window).

To find out where the problem occurred, I tried running my app while changing only one setting at a time.

The error message mentioned the Java version, so I went to File > Project Structure > Module. Here, I changed two places as follows according to the codelab projects’s settings (each was originally JavaVersion.VERSION_1_8):

Source Compatibility:
$JavaVersion.VERSION_17
Target Compatibility:
$JavaVersion.VERSION_17

Sync Now > Run. An error occurred, but the message was changed:

Execution failed for task ':app:compileDebugKotlin'.
> 'compileDebugJavaWithJavac' task (current target is 17) and 'compileDebugKotlin' task (current target is 1.8) jvm target compatibility should be set to the same Java version.

In each of the two projects, I clicked the Project tab on the left side. I selected Android from the drop-down menu. There are several configuration files here. I compared them one by one. As expected, build.gradle.kts (:app) was the culprit. In this file I made the following changes (originally it was 1.8 instead of 17):

kotlinOptions {
        jvmTarget = "17"
    }

Sync Now > Run. An error occurred, but the message was changed once more:

This version (1.4.3) of the Compose Compiler requires Kotlin version 1.8.10 but you appear to be using Kotlin version 1.8.21 which is not known to be compatible.  Please consult the Compose-Kotlin compatibility map located at https://developer.android.com/jetpack/androidx/releases/compose-kotlin to choose a compatible version pair (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).

Referring to this message, I changed build.gradle.kts (:app) as follows (it was originally 1.4.3 instead of 1.4.7):

composeOptions {
        kotlinCompilerExtensionVersion = "1.4.7"
    }

Refer to the site in the message. https://developer.android.com/jetpack/androidx/releases/compose-kotlin

Sync Now > Run. > The app runs normally.

Summary

The numbers above applies to my situation, so people should apply them according to their own situation.

One-line summary: When this kind of problem occurs, it can be easily solved by comparing the settings between your project and other project that runs well.

Leave a Comment