无法在 build.grade 中设置 Room

Failure to setup Room in build.grade

我正在尝试配置 build.grade 以使用 Room

错误仍然存​​在:

Unresolved reference: room

因此,注释仍未解决

@Entity

7个教程3天,只好投降求助

这里我粘贴我的build.grade。我从这个 google 教程中得到它:

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

顺便说一下,这与另一个 google 教程完全不同:

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

我做错了什么?

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"

}```



我已经将这些依赖项用于他们为我找到的 ROOM

kapt "androidx.room:room-compiler:2.4.2"
implementation "androidx.room:room-runtime:2.4.2"
implementation "androidx.room:room-ktx:2.4.2"
androidTestImplementation "androidx.room:room-testing:2.4.2"

这些是插件:

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

}

P.S如果你能分享错误代码就更好了,你也可以尝试删除打包选项

感谢 Mike T 和 Dkathayat1,它成功了。我必须设置正确的版本。

代码如下:

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"


}

在模块 gradle 文件的插件中添加 kotlin-dsl 解决了我的问题。

plugins { 'kotlin-dsl' }