【问题标题】:Failure to setup Room in build.grade在 build.gradle 中设置 Room 失败
【发布时间】:2022-08-14 13:02:55
【问题描述】:

我正在尝试将 build.gradle 配置为使用 Room

错误仍然存​​在:

Unresolved reference: room

因此,注释仍然未解决

@Entity

经过7个教程和3天,我不得不投降并寻求帮助。

在这里我粘贴我的 build.grade。我从这个谷歌教程中得到了它:

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

顺便说一句,这与其他谷歌教程完全不同:

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\"

}```



  • 你注意评论了吗// You\'ll probably have to update the version numbers in build.gradle (Project)并设置roomVersion因此?如果不是$rootProject.roomVersion,您可以在上面编写实际版本,例如2.4.2,因此您可以使用implementation \"androidx.room:room-ktx:2.4.2\" 而不是implementation \"androidx.room:room-ktx:$rootProject.roomVersion\"。第二个链接包括在 build.gradle 中设置 roomVersion 值

标签: android-studio kotlin android-room


【解决方案1】:

我已将这些依赖项用于他们为我工作的 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 如果您分享了错误代码,那就太好了。

【讨论】:

    【解决方案2】:

    感谢 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"
    
    
    }
    
    

    【讨论】:

      【解决方案3】:

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

      plugins { 'kotlin-dsl' }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-31
        • 1970-01-01
        • 1970-01-01
        • 2021-03-20
        • 2016-07-10
        • 2015-10-15
        • 2023-01-18
        相关资源
        最近更新 更多