Room数据库Schema export Error

Kornan
2021-04-02 / 0 评论 / 848 阅读 / 正在检测是否收录...

在使用Android Room数据库的时候报错了:

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.

如果您不需要检查架构并且想要摆脱警告,只需将exportSchema = false添加到RoomDatabase中,如下所示:

@Database(entities = { YourEntity.class }, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
   //...
}

另一种方法是在应用模块的build.gradle文件中,将此文件添加到defaultConfig部分(在android部分下面)。这会将架构写到项目文件夹的schemas子文件夹中。

android {

    // ... (compileSdkVersion, buildToolsVersion, etc)

    defaultConfig {

     // ... (applicationId, miSdkVersion, etc)

        kapt {
            arguments {
                arg("room.schemaLocation","$projectDir/schemas".toString())
            }
        }
    }

    buildTypes {
        // ... (buildTypes, compileOptions, etc)
    }
}

//...

以上是Kotlin,如果是Java:

android {

    // ... (compileSdkVersion, buildToolsVersion, etc)

    defaultConfig {

        // ... (applicationId, miSdkVersion, etc)

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation":"$projectDir/schemas".toString()]
            }
        }
    }

    // ... (buildTypes, compileOptions, etc)

}

当执行项目后,在Android Studio 的Project视图下,查看项目,会发现Module生成了一个schemas的文件夹,打开.json文件看起来像这样:

{
 "formatVersion": 1,
 "database": {
   "version": 1,
   "identityHash":"6240057b6178b803a0bf9915edf969e3",
   "entities": [
      {
       "tableName":"sms_table",
       "createSql":"CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `message` TEXT, `date` INTEGER, `client_id` INTEGER)",
       "fields": [
          {
           "fieldPath":"id",
           "columnName":"id",
           "affinity":"INTEGER"
          },
          {
           "fieldPath":"message",
           "columnName":"message",
           "affinity":"TEXT"
          },
          {
           "fieldPath":"date",
           "columnName":"date",
           "affinity":"INTEGER"
          },
          {
           "fieldPath":"clientId",
           "columnName":"client_id",
           "affinity":"INTEGER"
          }
        ],
       "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, "6240057b6178b803a0bf9915edf969e3")"
    ]
  }
}
0

评论

博主关闭了当前页面的评论