android-studiokotlinandroid-room

Failure to setup Room in build.grade


I'm trying to configure the build.grade to use Room

The error persists:

Unresolved reference: room

And, consequently, the anotation remains Unresolved

@Entity

After 7 tutorials and 3 days, I have to surrender and ask for help.

Here I paste my build.grade. I've got it from this google tutorial:

https://developer.android.com/codelabs/android-room-with-a-view-kotlin#3

Which, by the way, is completely diferent from this other google tutorial:

https://developer.android.com/training/data-storage/room

What am I doing wrong?

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

}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.firewalker.mouth_conjugation"
        minSdk 21
        targetSdk 32
        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'
    }

    packagingOptions {
        exclude 'META-INF/atomicfu.kotlin_module'
    }


}

dependencies {


    implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
    implementation "androidx.activity:activity-ktx:$rootProject.activityVersion"

    // Dependencies for working with Architecture components
    // You'll probably have to update the version numbers in build.gradle (Project)

    // Room components
    implementation "androidx.room:room-ktx:$rootProject.roomVersion"
    kapt "androidx.room:room-compiler:$rootProject.roomVersion"
    androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$rootProject.lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-common-java8:$rootProject.lifecycleVersion"

    // Kotlin components
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"

    // UI
    implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"
    implementation "com.google.android.material:material:$rootProject.materialVersion"

    // Testing
    testImplementation "junit:junit:$rootProject.junitVersion"
    androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
    androidTestImplementation ("androidx.test.espresso:espresso-core:$rootProject.espressoVersion", {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    androidTestImplementation "androidx.test.ext:junit:$rootProject.androidxJunitVersion"

}```




Solution

  • Thanks to Mike T and Dkathayat1, it worked. I had to set the correct versions.

    The code is the folowing:

    plugins {
        id 'com.android.application'
        id 'org.jetbrains.kotlin.android'
        id 'kotlin-kapt'
    
    }
    
    android {
        compileSdk 32
    
        defaultConfig {
            applicationId "com.firewalker.mouth_conjugation"
            minSdk 21
            targetSdk 32
            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'
        }
    
        packagingOptions {
            exclude 'META-INF/atomicfu.kotlin_module'
        }
    
    
    }
    
    dependencies {
    
    
        implementation "androidx.appcompat:appcompat:1.4.1"
        implementation "androidx.activity:activity-ktx:1.4.0"
    
        // Dependencies for working with Architecture components
        // You'll probably have to update the version numbers in build.gradle (Project)
    
        // Room components
        implementation "androidx.room:room-ktx:2.4.2"
        implementation 'androidx.room:room-common:2.4.2'
        kapt "androidx.room:room-compiler:2.4.2"
        androidTestImplementation "androidx.room:room-testing:2.4.2"
    
        // Lifecycle components
        implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"
        implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.1"
        implementation "androidx.lifecycle:lifecycle-common-java8:2.4.1"
    
        // Kotlin components
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.30"
        api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9"
        api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9"
    
        // UI
        implementation "androidx.constraintlayout:constraintlayout:2.0.2"
        implementation "com.google.android.material:material:1.2.1"
    
        // Testing
        testImplementation "junit:junit:4.13.1"
        androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
        androidTestImplementation ("androidx.test.espresso:espresso-core:3.1.0", {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        androidTestImplementation "androidx.test.ext:1.1.2"
    
    
    }