Search code examples
androidkotlinandroid-room

Trying to create room database with android, but keep getting dependency error


Android.com says to add these dependencies to your gradle file. I keep getting an error on the lines that start with kapt and ksp saying could not find those methods. Any ideas? Sorry I am not smart.

//below is what android says to put in

def room_version = "2.3.0"

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 Symbolic 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:2.4.0-alpha04")

Build file 'C:\Users\tanne\AndroidStudioProjects\LazyAlarm\app\build.gradle' line: 51

A problem occurred evaluating project ':app'.

Could not find method kapt() for arguments [androidx.room:room-compiler:2.3.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

////////////////////////////////////////// //gradle file below

plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}

android {
compileSdk 31

defaultConfig {
    applicationId "com.example.lazyalarm"
    minSdk 22
    targetSdk 31
    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'
}
}

 dependencies {

implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.room:room-ktx:2.3.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
def room_version = "2.3.0"

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 Symbolic 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:2.4.0-alpha04")

}

Solution

  • You appear to have copied a bunch of lines into your build.gradle file verbatim from Google's documentation. Unfortunately, their documentation has issues. In particular, many of those lines represent choices, and you need to choose which ones you need. In particular, pick exactly one of these two lines:

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

    ...and pick one of these four lines:

    implementation("androidx.room:room-ktx:$room_version")
    implementation "androidx.room:room-rxjava2:$room_version"
    implementation "androidx.room:room-rxjava3:$room_version"
    implementation "androidx.room:room-guava:$room_version"
    

    Then, depending on your choice from that first pair, you may need to add another plugin to your plugins block at the top of the file. If you choose the kapt line, you need to add id 'kotlin-kapt'. I have not used the ksp option and do not know what plugin you need for it, if any.