[Fixed] Execution failed for task ':app:kaptGenerateStubsDebugKotlin' – Kapt

by
Ali Hasan
android kapt

Quick Fix: Update your project’s build.gradle file to set the sourceCompatibility, targetCompatibility, and jvmTarget options to Java version 18. Additionally, ensure that you are using JDK 18 for your project.

The Problem:

After updating Android Studio from ElectricEel to Flamingo, the inclusion of the id 'kotlin-kapt' plugin causes a build error.

The error message is:

'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.
Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:kaptGenerateStubsDebugKotlin'.
	...```

This error occurs because the `compileDebugJavaWithJavac` task is using Java version 1.8, while the `kaptGenerateStubsDebugKotlin` task is using Java version 17. The jvm target compatibility for both tasks must be set to the same Java version.

To resolve this issue, add the following to your build.gradle file:

```kotlinOptions {
    jvmTarget = '1.8'
}```

This will set the jvm target for the `kotlinOptions` task to 1.8, which will match the jvm target for the `compileDebugJavaWithJavac` task.

You can also use a JVM toolchain to specify the Java version for the `kaptGenerateStubsDebugKotlin` task. To do this, add the following to your build.gradle file:

```kotlin {
    jvmToolchain(1.8)
}```

This will set the JVM toolchain for the `kotlin` task to 1.8, which will match the jvm target for the `compileDebugJavaWithJavac` task.

The Solutions:

Solution 1: Update Gradle JDK to version 18

In order to resolve this issue, update your project’s Gradle JDK to version 18.

To do so, add the following to your gradle-wrapper.properties file:

distributionUrl=https://services.gradle.org/distributions/gradle-8.0-bin.zip

Then, restart your IDE.

Solution 2: Error while running kapt task

When the Kotlin Gradle plugin is updated, the error may appear due to incompatibility between the `compileDebugJavaWithJavac` and `kaptGenerateStubsDebugKotlin` tasks. Both should have the same target compatibility for Java.

To resolve this, consider using a JVM toolchain as recommended by the error message: https://kotl.in/gradle/jvm/toolchain

Alternatively, you can modify your app level build.gradle as follows:

android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
    ...
}

This sets the minimum Java version for the Kotlin Gradle plugin to 17, resolving the compatibility issue.

Solution 3: Use Java Toolchain

To resolve this issue, consider using the JVM toolchain by adding the following to your build.gradle file:

android {
  kotlin {
    jvmToolchain(17)
  }
}

This ensures that both compileDebugJavaWithJavac and kaptGenerateStubsDebug tasks use the same Java version, which is required for compatibility.

The error message suggests that the compileDebugJavaWithJavac task uses Java 1.8 while the kaptGenerateStubsDebug task uses Java 17. By setting the JVM toolchain to 17, you ensure that both tasks use the same version of Java.

Video Explanation:

The following video, titled "How to fix Execution failed for task ':app ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

AndroidStudio #Howtofix #Nomatchingclientfoundforpackagename #packagename #Executionfailedfortask You can fix error Execution failed for ...