繁体   English   中英

无法导入 Room 数据库以在 Android 应用程序中使用

[英]Unable to import Room database for use in Android app

我正在通过 android 工作室开发 Android 应用程序,并且我正在尝试实现 Room 数据库。 I was using https://developer.android.com/training/data-storage/room for instructions as well as https://medium.com/mindorks/using-room-database-android-jetpack-675a89a0e942 , however I was无法正确导入。尝试import androidx.room.*; 返回cannot resolve symbol 'room' 我无法找到解决此问题的方法。

我努力了:

  • 使用https://maven.google.com作为存储库
  • 使用不同版本的房间
  • 使用api代替implementation

项目 Gradle 文件:

allprojects {
    repositories {
        jcenter()
        google()
    }
}        

应用程序 Gradle 文件:

dependencies {
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    def room_version = "2.2.6"
    implementation "android.arch.persistence.room:runtime:$room_version"
    annotationProcessor "android.arch.persistence.room:compiler:$room_version"
}        

编辑:我也试过这个没有成功,以及使用 room_version 作为 1.0.0

def room_version = "2.2.6"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

编辑:在 gradle 同步时,我发现它在Download room-runtime-2.2.6.pom...卡住了 10-15 分钟,完成后显示警告:

Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Could not resolve androidx.room:room-runtime:2.2.6.
Show Details
Affected Modules: app

Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve androidx.room:room-runtime:2.2.6.
Show Details
Affected Modules: app

Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve androidx.room:room-runtime:2.2.6.
Show Details
Affected Modules: app

单击显示详细信息并没有将其定向到任何地方

您需要使用androidx依赖项。 您已经在使用androidx工件,因此您必须对Room使用相同的工件。 在文档中也提到了它。

def room_version = "2.2.6"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM