在使用 Android Room資料庫的時候,雖然項目可以運作起來。
但是,卻報以下error:
Error:(, ) 警告: Schema export directory is not provided to the annotation processor so we cannot export the schema.
You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
實際上,上面已經提示了開發者應該怎麼處理,給出了兩種方案。
解決方式一:
給RoomDatabase設定exportSchema注解為false。
@Database(entities = { YourEntity.class }, version = , exportSchema = false)
public abstract class MovieDatabase extends RoomDatabase {
...
}
解決方案二:
在項目中gradle中通過 annotationProcessorOptions 注解,為
room.schemaLocation
指定schemas的子檔案夾。
android {
compileSdkVersion
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.xingen.architecturecomponents"
minSdkVersion
targetSdkVersion
versionCode
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//指定room.schemaLocation生成的檔案路徑
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
}
當執行項目後,在Android Studio 的Project視圖下,檢視項目,會發現Module生成了一個schemas的檔案夾,如下圖所示:

其中,會生成版本1的Json檔案,這裡可以檢視Room資料庫的配置情況:
{
"formatVersion": ,
"database": {
"version": ,
"identityHash": "8240057b6178b803a0bf9915edf969ef",
"entities": [
{
"tableName": "movies",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `year` TEXT, `title` TEXT, `image` TEXT)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "year",
"columnName": "year",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "image",
"columnName": "image",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"8240057b6178b803a0bf9915edf969ef\")"
]
}
}
以上項目案例的連接配接: https://github.com/13767004362/ArchitectureComponentsDemo
資源參考:
- https://stackoverflow.com/questions/44322178/room-schema-export-directory-is-not-provided-to-the-annotation-processor-so-we