[Fixed] Android Studio Build Error: compileDebugJavaWithJavac and kaptGenerateStubsDebugKotlin Task – Android

Photo of author
Written By M Ibrahim
android androidgradleplugin androidstudio gradle kotlin

When building an Android Studio project, you may come across a build error related to the compileDebugJavaWithJavac and kaptGenerateStubsDebugKotlin tasks. The error message states:

compileDebugJavaWithJavac task (current target it 1.8) and kaptGenerateStubsDebugKotlin task (current target is 17) jvm target compatibility should be set to the same java version.

This error occurs when there is a mismatch in the JVM target compatibility settings between these tasks. Fortunately, there are several solutions that can help resolve this issue. Let’s explore them:

Solution 1: Update Gradle plugin version

One possible solution is to update the Gradle plugin version in your project’s `build.gradle` file. Open the `build.gradle` file for the project (not the module) and modify the `dependencies` section to include the latest Gradle plugin version:

dependencies {
    classpath 'com.android.tools.build:gradle:4.1.0'
    // ...
}

After making this change, sync your project with the Gradle files by clicking on the "Sync Now" button in the Android Studio toolbar. This should update the Gradle plugin and resolve the JVM compatibility issue.

Solution 2: Set JVM target compatibility in build.gradle

Another solution is to explicitly set the JVM target compatibility in your project’s `build.gradle` file. Open the `build.gradle` file for the module where the error is occurring and add the following lines:

android {
    ...
    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

These lines set both the source and target compatibility to Java 17 and the JVM target compatibility to Java 1.8. Sync your project with the Gradle files to apply the changes.

Solution 3: Use JVM toolchain

If you are using Kotlin in your project, you can also consider using the JVM toolchain. This allows you to specify the Java version using a toolchain block in your `build.gradle` file. Here’s an example:

android {
    ...
    
    kotlin {
        jvmToolchain {
            languageVersion.set(JavaLanguageVersion.of(17))
        }
    }
}

Using the JVM toolchain ensures that the Java version is consistent across all tasks and dependencies.

Conclusion

Encountering the “compileDebugJavaWithJavac” and “kaptGenerateStubsDebugKotlin” JVM target compatibility issue in Android Studio can be frustrating. In this article, we explored three possible solutions to resolve the error. By either updating the Gradle plugin version, setting JVM target compatibility in the `build.gradle` file, or using the JVM toolchain, you can ensure that the Java version is consistent and eliminate the compatibility issue.

Remember to sync your project with the Gradle files after implementing any changes to ensure they take effect.

Keep coding and happy building!

Image source

Official Gradle Plugin Documentation

Official Kotlin Documentation