Search code examples
kotlinandroid-studiogradlegroovy

Android studio error: Execution failed for task ':app:kaptGenerateStubsDebugKotlin'


After add kotlin-kapt plugin to my project this error accrued:

Execution failed for task ':app:kaptGenerateStubsDebugKotlin'.
  '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 
    Run with --stacktrace option to get the stack trace.
    Run with --info or --debug option to get more log output.
    Run with --scan to get full insights.

this is my build.gradle file

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
}

android {
    namespace 'com.example.roomtrain'
    compileSdk 34

    defaultConfig {
        applicationId "com.example.roomtrain"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        dataBinding true
    }
}

dependencies {

    def room_version = "2.5.2"

    implementation 'androidx.core:core-ktx:1.10.1'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    kapt "androidx.room:room-compiler:$room_version"

    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'


    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'


} 

and this is my build.gradle(Project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '8.0.2' apply false
    id 'com.android.library' version '8.0.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
}

I change version of JDK but same error accrued again.


Solution

    1. First of all, try downloading the AGP version to 7.4.2 each (any version is fine as long as it is less than 8.0.0 each)

    2. Add the code to the build.gradle.kts of each module where the problem occurs and specify the jvmTarget version to build it.

      compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 }

      kotlinOptions { jvmTarget = JavaVersion.VERSION_17.toString() }

    If it's not an Android module, but a Kotlin or Java library module, just add this.

    java {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    

    The jvmToolchain version will be built normally without specifying it separately.