android - 无法为 org.gradle.api.Project 类型的根项目 'roomVersion' 获取未知属性 'RoomWordSample'

标签 android android-room

我正在尝试从 https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin/#3 了解 Android 房间
第四部分是关于更新 Gradle 文件。它提供了几个代码 fragment 以包含在

build.gradle (Module: app)


我已经包含了页面上提到的所有实现、插件等。
   apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.testing.roomwordsample"
        minSdkVersion 18
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

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


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

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

// Lifecycle components
    implementation "androidx.lifecycle:lifecycle-extensions:$rootProject.archLifecycleVersion"
    kapt "androidx.lifecycle:lifecycle-compiler:$rootProject.archLifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.archLifecycleVersion"

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

// Material design
    implementation "com.google.android.material:material:$rootProject.materialVersion"

// Testing
    testImplementation 'junit:junit:4.12'
    androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
}

ext {
    roomVersion = '2.2.5'
    archLifecycleVersion = '2.2.0'
    coreTestingVersion = '2.1.0'
    materialVersion = '1.1.0'
    coroutines = '1.3.4'
}
但是构建失败并给出了这个错误。

Could not get unknown property 'roomVersion' for root project 'RoomWordSample' of type org.gradle.api.Project.


这就是构建窗口显示的内容。
Error window

最佳答案

Codelab 说明的第 5 步表明您应该将包含版本字符串的 ext block 添加到 。项目 build.gradle .您已将其添加到应用模块 build.gradle .
从应用模块 build.gradle 中剪切此文本并将其粘贴到项目中build.gradle :

ext {
    roomVersion = '2.2.5'
    archLifecycleVersion = '2.2.0'
    coreTestingVersion = '2.1.0'
    materialVersion = '1.1.0'
    coroutines = '1.3.4'
}

关于android - 无法为 org.gradle.api.Project 类型的根项目 'roomVersion' 获取未知属性 'RoomWordSample',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62633236/

相关文章:

android - 房间迁移 - 将 INTEGER 列类型更改为可为空 (SQLITE)

java - 为什么 Android Room @Query LIKE 不返回已知存在的数据

java - 如何使用房间数据库使进度条影响 UI 上的数据?

java - webview 中的 Youtube 显示桌面版的 youtube

android - 突然执行 aapt 时出错

android - MvxCachingFragmentStatePagerAdapter 传递参数给viewmodel

android - 以编程方式将按钮的宽度设置为 LinearLayout