Room持久化库:无法解析符号 Room

4
我使用的是Android Studio 3.0 Beta 2版本,并采用以下gradle设置。我搜索了RxRoom,但未找到Room类。在按照添加组件的说明操作后,我无法在Fragment中使用Room类。这个类在一个单独的模块中,应用程序模块不包含任何room库依赖项。IDE中显示错误“无法解析符号Room”。enter image description here,build.gradle。
apply plugin: 'com.android.library'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"


    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation":
                                     "$projectDir/schemas".toString()]
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.0.2'
    implementation 'com.android.support:support-v4:26.0.2'
    implementation 'com.android.support:recyclerview-v7:26.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation "android.arch.persistence.room:runtime:1.0.0-alpha9"
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha9"
    testImplementation "android.arch.persistence.room:testing:1.0.0-alpha9"
    implementation "android.arch.persistence.room:rxjava2:1.0.0-alpha9"
}

更新 我尝试先将 Room 库降级至 alpha8 版本,现在 Room 类已经恢复了。我不知道为什么在 alpha9 版本中这个类会消失,但至少这是一个解决方法。

3个回答

6
在试用了一周之后,我发现即使我在编译时出现错误,仍然可以运行apk。我尝试通过以下方法解决编译错误问题:

更改

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

为了

 implementation "android.arch.persistence.room:compiler:1.0.0-alpha9"

并添加以下内容从迁移到Gradle 3.0的Android插件中禁用错误检查

javaCompileOptions {
    annotationProcessorOptions {
        includeCompileClasspath = true
    }
}

我有同样的问题。我的代码是: api "android.arch.persistence.room:runtime:1.0.0" annotationProcessor "android.arch.persistence.room:compiler:1.0.0"但编译是正常的。仍然无法解决“无法解析符号Room”的问题。 - Tony
annotationProcessor改为implementation就可以了。 - MrObjectOriented

1
请在build.gradle中将所有Room依赖项从implementation更改为api

我将运行时和RxJava2更改为“api”,但没有成功。 - Long Ranger

1
如果在您的Room实体类上出现“无法解析符号”错误,请尝试以下方法:向类添加一个虚拟属性,然后清理并重新构建项目。这将刷新实体。然后删除虚拟属性。
示例:
@Query("select * from my_entity where name = :name") --> Cannot resolve symbol name...
MyEntity getMyEntityByName(String name);  


@Entity(indices = {@Index(value = {"name"})}, tableName = "my_entity")
public class MyEntity {
    other properties        ....

    public int dummy; --> add and remove this

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接