Search code examples
androiddependencies

how to add room dependencies in android studio?


I'm trying to add these dependencies but other dependencies using libs objects , I watched multiple YouTube videos none pf them use libs obj and they just add dependencies without problems. here is the dependencies I want to add :

 // Navigation Component
    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'

    // Room components
    implementation "androidx.room:room-runtime:2.2.5"
    kapt "androidx.room:room-compiler:2.2.5"
    implementation "androidx.room:room-ktx:2.2.5"
    androidTestImplementation "androidx.room:room-testing:2.2.5"

    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"

    // Kotlin components
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.72"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5"

I tried to add it just with others like this but didn't work :

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
  ....
    implementation(kotlin("script-runtime"))
    //added 
    // Navigation Component
    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'

    // Room components
    implementation "androidx.room:room-runtime:2.2.5"
    kapt "androidx.room:room-compiler:2.2.5"
    implementation "androidx.room:room-ktx:2.2.5"
    androidTestImplementation "androidx.room:room-testing:2.2.5"

}

Solution

  • About "libs object": Well version catlog is something that was introduced recently as of 2024, which is good on it's own for maintaining the readability of dependencies on both app/project levels.

    But talking about adding room dependencies, in YouTube videos which are using it's older as per the way you are describing it. So it's written in Groovy, whereas your is written in Kotlin.

    Now to implement room dependencies is pretty much self-explanatory on the internet... https://developer.android.com/jetpack/androidx/releases/room?authuser=1#kts

    But I'll try my best, in app level build.gradle add this :-

     // Navigation Component
    dependencies {
    implementation("androidx.navigation:navigation-fragment-ktx:2.2.2")
    implementation("androidx.navigation:navigation-ui-ktx:2.2.2")
    
    // Room components
    implementation("androidx.room:room-runtime:2.2.5")
    kapt("androidx.room:room-compiler:2.2.5")
    implementation("androidx.room:room-ktx:2.2.5")
    androidTestImplementation("androidx.room:room-testing:2.2.5")
    
    // Lifecycle components
    implementation("androidx.lifecycle:lifecycle-extensions:2.2.0")
    implementation("androidx.lifecycle:lifecycle-common-java8:2.2.0")
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0")
    
    // Kotlin components
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.72")
    api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5")
    api("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5")
    }
    

    If you sync this, you still might get an "kapt" error, because you have to import that first sync then import the rest.

    But (since you are only checking for ROOM), it's highly recommended you use KSP (kotlin symbol processor) instead of KAPT (kotlin annotation processor tool) for faster annotation processing.

    In project level, build.gradle, under plugins:-

    id("com.google.devtools.ksp") version "{version of kotlin being used in your project}-{compatible stable version of ksp}" apply false
    

    https://github.com/google/ksp/releases -> to check the versions (example 2.0.0-1.0.23)

    To check what version of kotlin you are using check libs.versions.toml in your project

    Now in app level build.gradle add this:-

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

    And replace kapt dependency with ksp:-

    ksp("androidx.room:room-compiler:2.2.5")