【问题标题】:Cannot import Room database for use in Android app无法导入 Room 数据库以在 Android 应用程序中使用
【发布时间】:2021-02-27 03:09:57
【问题描述】:

我正在通过 android studio 开发一个 Android 应用程序,并且我正在尝试实现一个 Room 数据库。我使用https://developer.android.com/training/data-storage/room 进行说明,但是我无法正确导入它。尝试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 "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

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

编辑:我通过更改应用程序 build.gradle 文件以将 https://maven.google.com 存储库用于 buildscript 和 allprojects 来解决问题,以便我的 gradle 文件可以读取

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

还有

buildscript {
    repositories {
        maven {
            url "https://maven.google.com"
        }
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

【问题讨论】:

    标签: java android android-studio android-room


    【解决方案1】:

    您的项目是否已迁移到 androidx 中?

    使用 Android Studio 迁移现有项目

    使用 Android Studio 3.2 及更高版本,您可以通过从菜单栏中选择 Refactor > Migrate to AndroidX 将现有项目迁移到 AndroidX。

    重构命令使用两个标志。默认情况下,两者 在您的 gradle.properties 文件中设置为 true:

     android.useAndroidX=true
     android.enableJetifier=true
    

    Android 插件使用适当的 AndroidX 库而不是支持库。

    来源:https://developer.android.com/jetpack/androidx/migrate

    【讨论】:

    • 我的 gradle.properties 文件已经将 android.useAndroidX 和 android.enableJetifier 标志设置为 true,我应该迁移它们吗?编辑:尝试迁移它状态“在项目中找不到任何用法”
    【解决方案2】:

    您必须从设置中检查下载外部注释以获取依赖项

    setp 1 - 转到文件

    setp 2 -点击设置

    setp 3 - 点击构建、执行、部署

    setp 4 -点击Gradle

    第 5 步 - 检查下载依赖项的外部注释

    【讨论】:

    • 我的项目中已经启用了这个选项
    • 你的类路径“com.android.tools.build:gradle:4.1.2”的版本是什么
    • 我认为房间库没有成功下载,请检查您的互联网连接,然后再次尝试实现库
    • 我的类路径是“com.android.tools.build:gradle:4.1.2”,我的互联网连接是 100% 正常的,我已经在不同的日子尝试过
    • 你让android stuido失效并重启
    猜你喜欢
    • 1970-01-01
    • 2018-11-27
    • 2015-12-24
    • 2019-12-30
    • 2013-05-17
    • 2012-05-29
    • 2016-04-17
    • 2022-06-29
    相关资源
    最近更新 更多