Problem with chapter 12 (Coroutine and Room)

Hello,
chapter 12 in the end of section “Querying the Database” we asked for run the app.
while I’m start running the app i get an error:

"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: Configure a Gradle project | Kotlin Documentation"

and after a week that i tried to solved that with no success i want to ask you guys If anyone comes across this problem?

If so, how are you solved that?

this is my build.gradle file (app level):

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("org.jetbrains.kotlin.kapt")
}
android {
    namespace = "com.example.criminalIntent3"
    compileSdk = 33

    defaultConfig {
        applicationId = "com.example.criminalIntent3"
        minSdk = 21
        targetSdk = 33
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        targetCompatibility = JavaVersion.VERSION_1_8
        sourceCompatibility = JavaVersion.VERSION_1_8

    }
    kotlinOptions {
        jvmTarget = "1.8"
    }

    buildFeatures {
        viewBinding = true
    }
}

dependencies {
    implementation("androidx.core:core-ktx:1.9.0")
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.9.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    implementation("androidx.lifecycle:lifecycle-extensions:2.0.0")
    implementation("androidx.recyclerview:recyclerview:1.2.1")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.4.0")
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0")
    implementation("androidx.room:room-runtime:2.4.2")
    implementation("androidx.room:room-ktx:2.4.2")
    kapt("androidx.room:room-compiler:2.4.2")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}

Thanks :slight_smile:

Check to see what version of Kotlin you are using. The book assumes that you are using 1.6.10, but there have been a handful of releases since the book was printed.

I got this error after upgrading my gradle plugin. More specifically a org.gradle.api.tasks.TaskExecutionException

Searcjing I found the following. Paste this in build.gradle. I have it above my dependencies:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs).configureEach {
kotlinOptions {
jvmTarget = “1.8”
}
}

and if you migrated to kotlin dsl:

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = “1.8”
}
}

i have no idea what this does. I just didn’t feel like learning at this point the finer details of the build process. I hope this works for you too.

I’m using Kotlin version of 1.8.0
change it to 1.6.10?

thanks for your answer.

i pasted this part above my dependecies:

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = “1.8”
}
}

and i get now this error:

C:\Users\user\CriminalIntent3\app\build\tmp\kapt3\stubs\debug\com\example\criminalintent3\database\CrimeDao.java:11: error: Not sure how to convert a Cursor to this method's return type (java.lang.Object).
    public abstract java.lang.Object getCrimes(@org.jetbrains.annotations.NotNull

i think it related to this data class:
package com.example.criminalintent3

import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.Date
import java.util.UUID

@Entity
data class Crime(
    @PrimaryKey val id: UUID,
    val title: String,
    val date: Date,
    val isSolved: Boolean
)

but I don’t understand why.

I think it would make sense to try and use the latest versions of your dependencies. Go through your dependencies block in the build.gradle files and update those libraries. maven.google.com is a good tool for finding the latest version of a particular library (such as Room).

Let me know how that goes for you.