Android 架构组件之Room数据库 处理Schema export Error

在使用 Android Room数据库的时候,虽然项目可以运行起来。

但是,却报以下error:

Error:(22, 17) 警告: 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 = 1, exportSchema = false)
public abstract class MovieDatabase extends RoomDatabase {

   ...

}

解决方案二

在项目中gradle中通过 annotationProcessorOptions 注解,为room.schemaLocation指定schemas的子文件夹。

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.xingen.architecturecomponents"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        //指定room.schemaLocation生成的文件路径
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }
    }

}

当执行项目后,在Android Studio 的Project视图下,查看项目,会发现Module生成了一个schemas的文件夹,如下图所示:

Android 架构组件之Room数据库 处理Schema export Error_第1张图片

其中,会生成版本1的Json文件,这里可以查看Room数据库的配置情况:

{
  "formatVersion": 1,
  "database": {
    "version": 1,
    "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

你可能感兴趣的:(Android,项目架构(MVP模式,组件化,架构组件,插件化),components,android,schema,room,数据库)