androidkotlinandroid-studioandroid-roomkapt

Unresolved reference kapt and ksp in Android Studio when trying to do the setup for the use of ROOM local database


I've recently started to learn programming in Android Studio Kotlin and after going through some tutorials on Kotlin language and creating a User Interface in Android Studio, I decided to start learning about how to use the ROOM database. After some trials to do the setup by looking on the Internet on different tutorials on youtube or various blogs I finally decided to take the more formal approach and read through the documentation. However, to my disapointment, after I managed to get rid of some errors I still stumbled on the following error: "Unresolved reference kapt" and "Unresolved reference ksp" in my build.gradle.kts (Module:app) file. The project I work on is completely new excepting the modifications I added in the aformentioned file. I add the contents of the file below.

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
    namespace = "com.example.useofroomdatabase"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.example.useofroomdatabase"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = 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 {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {

    implementation("androidx.core:core-ktx:1.12.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
    implementation("androidx.activity:activity-compose:1.8.2")
    implementation(platform("androidx.compose:compose-bom:2023.08.00"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.08.00"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")

    val room_version = "2.6.1"

    implementation("androidx.room:room-runtime:$room_version")
    annotationProcessor("androidx.room:room-compiler:$room_version")

    // To use Kotlin annotation processing tool (kapt)
    kapt("androidx.room:room-compiler:$room_version")
    // To use Kotlin Symbol Processing (KSP)
    ksp("androidx.room:room-compiler:$room_version")

    // optional - Kotlin Extensions and Coroutines support for Room
    implementation("androidx.room:room-ktx:$room_version")

    // optional - RxJava2 support for Room
    implementation("androidx.room:room-rxjava2:$room_version")

    // optional - RxJava3 support for Room
    implementation("androidx.room:room-rxjava3:$room_version")

    // optional - Guava support for Room, including Optional and ListenableFuture
    implementation("androidx.room:room-guava:$room_version")

    // optional - Test helpers
    testImplementation("androidx.room:room-testing:$room_version")

    // optional - Paging 3 Integration
    implementation("androidx.room:room-paging:$room_version")
}

So, to sum up, I have 2 questions for my better elucidation. Firstly, what are these 2 concepts used for (kapt and ksp)? And secondly, what is the problem in my code and how can I solve it in order to further proceed with my practice? Thanks in advance.

P.S.: I inspired myself from this piece of documentation: https://developer.android.com/training/data-storage/room#kts

Later update: I finally managed to import the kapt module. I still don`t now how to get rid of the "unresolved reference" error for ksp module but from what I learned things should work too with kapt only, maybe slower but for now it's ok. I made the changes from the beginning of this link: https://www.youtube.com/watch?v=lkGIj1xSpFY

Finally, it might be something related to version compatibility between the modules and kotlin or other Android Studio related aspects which I'll further dig into.


Solution

  • using the kotlin-kapt with my issue:

    plugins {
      ...
       id("com.google.dagger.hilt.android")
       id("kotlin-kapt")
    }