简体   繁体   English

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

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

I am working on developing an Android app through android studio and I am trying to implement a Room database.我正在通过 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 unable to properly import it in. Trying import androidx.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.*; returns cannot resolve symbol 'room' .返回cannot resolve symbol 'room' I was unable to find a solution to this issue.我无法找到解决此问题的方法。

I have tried:我努力了:

  • Using https://maven.google.com as a repository使用https://maven.google.com作为存储库
  • Using different versions of room使用不同版本的房间
  • Using api instead of implementation使用api代替implementation

Project Gradle File:项目 Gradle 文件:

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

App Gradle File:应用程序 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"
}        

Edit: I have also tried this without success, as well as using room_version as 1.0.0编辑:我也试过这个没有成功,以及使用 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"

Edit: While gradle syncing, I found that it got stuck on Download room-runtime-2.2.6.pom... for 10-15 minutes, and once it completed it showed the warnings:编辑:在 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

Clicking show details did not direct it anywhere单击显示详细信息并没有将其定向到任何地方

You need to use androidx dependencies.您需要使用androidx依赖项。 you already using using androidx artifact so you have to use same for Room .您已经在使用androidx工件,因此您必须对Room使用相同的工件。 Also its mentioned in docs.在文档中也提到了它。

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