How to correctly add Room Persistence dependencies to my Jetack Compose Android app

633 Views Asked by At

I have been struggling to add Room dependencies for a couple of days now(not proud). There is always an error no matter what i try. I am not the most experienced though.

I added the below from the Android docs site.

`val room_version = "2.5.2"

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

//1
// To use Kotlin annotation processing tool (kapt)
kapt("androidx.room:room-compiler:$room_version")

//2
// To use Kotlin Symbol Processing (KSP) (2)
ksp("androidx.room:room-compiler:$room_version")

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

All depencdencies can resolve except what is marked 1 & 2. For 1, i get Type mismatch. Required: KaptOptions Found: String

For 2, i get Unresolved reference: ksp

I added these to my plugins from searches i did but to no avail. ` id("com.google.devtools.ksp") version "1.8.21-1.0.11" kotlin("android")

// Apply the Room and Kotlin Kapt plugins
kotlin("kotlin-kapt")
id("androidx.room.room-compiler")`
4

There are 4 best solutions below

0
Michael Appiah-kubi On BEST ANSWER

What properly worked was following the migration guide at Migrate from kapt to KSP

I used

ksp("androidx.room:room-compiler:$room_version")

Added the below to the project level build.grradle

plugins {
id("com.google.devtools.ksp") version "1.8.10-1.0.9" apply false}

Added the below to the module level build.gradle

plugins {
id("com.google.devtools.ksp")}

And remove everything added for the kapt

0
Gonzalo Ledezma Torres On

I had the same issue 3 months ago, my solution was this:

At the top of the build.gradle (:app) inside plugins

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

And then in your dependencies, same file

/* ROOM */
def roomVersion = "2.5.2" //this was 2.5.1 back then
implementation "androidx.room:room-ktx:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"

Finally, check if Android Studio wrote a Room dependency for you before and delete it:

implementation 'androidx.room:room-common:2.5.2'

Documentation is here

0
Denis Arkharov On
  1. Open your project build.gradle.
  2. Add the plugin com.google.devtools.ksp with the correct version:
    ...
    plugins {
        ...
        // x.y.z must be THE SAME for all the kotlin/ksp plugins.
        // (My version is 1.9.10)
        id "org.jetbrains.kotlin.jvm" version 'x.y.z' apply false
        id "org.jetbrains.kotlin.android" version 'x.y.z' apply false
        ...
        // Choose an appropriate version from: 
        // https://github.com/google/ksp/releases
        id "com.google.devtools.ksp" version 'x.y.z-a.b.c' apply false
    }
  1. Open your module build.gradle where you add room library.
  2. Add com.google.devtools.ksp to plugins:
    plugins {
        ...
        id 'com.google.devtools.ksp'
    }
  1. Replace kapt by ksp:
    ...
    dependencies {
        // delete kapt "androidx.room:room-compiler:$room_version"
        ksp "androidx.room:room-compiler:$room_version"
    }
0
RJA On

I too ran into issues. I can attest the migration page google provides will help you implement it. Below are the my gradle files. Along with a link to the latest release of 'ksp' from google.

Note

  • keep ksp("...") commented out until the build passes.
  • make sure to upgrade to the latest version, google docs have not updated it, causing an error message. Latest versions found here

Hope this helps, provided some code below, as I'm new to Android development.

build.gradle.kts:app

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

 .....

//Step 0: Dependencies

val lifecycle_version = "2.7.0"
val arch_version = "2.2.0"
val room_version = "2.5.0"

// ViewModel
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
// LiveData
implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")
// KSP
implementation("com.google.devtools.ksp:symbol-processing-api:1.9.21-1.0.15")

implementation("androidx.room:room-runtime:$room_version")
annotationProcessor("androidx.room:room-compiler:$room_version")
// To use Kotlin Symbol Processing (KSP) ***Keep commented until build passes
// ksp("androidx.room:room-compiler:$room_version")

build.gradle.kts(project)

plugins {
    id("com.android.application") version "8.2.1" apply false
    id("org.jetbrains.kotlin.android") version "1.9.0" apply false
    id("com.google.devtools.ksp") version "1.9.22-1.0.17" apply false
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath(kotlin("gradle-plugin", version = "1.9.21"))
    }
}