简体   繁体   中英

Cannot 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. I was using https://developer.android.com/training/data-storage/room for instructions, however I was unable to properly import it in. Trying import androidx.room.*; returns 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
  • Using different versions of room
  • Using api instead of implementation

Project Gradle File:

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

App Gradle File:

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

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:

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

Edit: I resolved the issue by changing the app build.gradle file to use the https://maven.google.com repository for both buildscript and allprojects such that my gradle file read

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

as well as

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
    }
}

did your project have been migrated into androidx?

Migrate an existing project using Android Studio

With Android Studio 3.2 and higher, you can migrate an existing project to AndroidX by selecting Refactor > Migrate to AndroidX from the menu bar.

The refactor command makes use of two flags. By default, both of them are set to true in your gradle.properties file:

 android.useAndroidX=true
 android.enableJetifier=true

The Android plugin uses the appropriate AndroidX library instead of a Support Library.

source: https://developer.android.com/jetpack/androidx/migrate

You have to Check Download external annotations for dependencies from setting

setp 1 - Go to File

setp 2 - Click on settings

setp 3 - Click on Build, Execution, Deployment

setp 4 - Click on Gradle

step 5 - Check Download external annotations for dependencies

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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