Android Room Database - Unresolved reference @Enti

2019-04-08 03:36发布

问题:

I am using Android Room Persistence library (v.1.0.0-alpha1) in my app. Although it is working fine, when I open model class (Kotlin Data class) in Android studio, it shows Unresolved reference for all annotations used for Room database like @Entity, @ColumnInfo etc. I tried changing version of arch library to 1.0.0-alpha5 but result was same.

In Lint inspection it is showing Remove deprecated symbol import for all imported annotations.AS was not showing this error previously.

How can I resolve this issue

Edit Following are imports I have added in my build.gradle

compile "android.arch.persistence.room:runtime:1.0.0-alpha5"

compile "android.arch.persistence.room:rxjava2:1.0.0-alpha5"

annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha5"

kapt "android.arch.persistence.room:compiler:1.0.0-alpha5"

回答1:

Here you have an example.

https://github.com/jsperk/PocRoom

Remember, you need add:

Gradle (Project)--> maven

Gradle (Module App) dependencies -->

implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
testImplementation "android.arch.persistence.room:testing:1.0.0"
implementation "android.arch.persistence.room:rxjava2:1.0.0"


回答2:

In my project, I have this question cause I'm using

android.arch.lifecycle:livedata:1.1.1

than no matter using room with version 1.1.1 or 1.0.0, it still can't find the android.arch.persistence.room.Entity.

I've searched for a long time, till I found that, when I delete the LiveData implementation, the problem solved. Then I notice that, the version of these two libraries conflict. At last, I use the same version of 1.1.0 for livedata and room (since livedata have no version of 1.0.0), and solved it.

def arch_version = "1.1.0"
implementation  "android.arch.persistence.room:runtime:$arch_version"
annotationProcessor "android.arch.persistence.room:compiler:$arch_version"
implementation "android.arch.persistence.room:rxjava2:$arch_version"
implementation "android.arch.persistence.room:common:$arch_version"
implementation "android.arch.lifecycle:livedata:$arch_version"
implementation "android.arch.lifecycle:extensions:$arch_version"