我使用的是Android Studio 3.0 Beta 2版本,并采用以下gradle设置。我搜索了RxRoom,但未找到Room类。在按照添加组件的说明操作后,我无法在Fragment中使用Room类。这个类在一个单独的模块中,应用程序模块不包含任何room库依赖项。IDE中显示错误“无法解析符号Room”。
,build.gradle。
![enter image description here](https://istack.dev59.com/shQfF.webp)
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 版本中这个类会消失,但至少这是一个解决方法。
api "android.arch.persistence.room:runtime:1.0.0" annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
但编译是正常的。仍然无法解决“无法解析符号Room”的问题。 - TonyannotationProcessor
改为implementation
就可以了。 - MrObjectOriented