簡體   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