Schema export directory is not provided to the annotation processor
我们在run app的过程中,系统提示如下问题:
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.
在这里,我们会看到一些相关的描述:
You can set annotation processor argument (room.schemaLocation) to tell Room to export the schema into a folder. Even though it is not mandatory, it is a good practice to have version history in your codebase and you should commit that file into your version control system (but don't ship it with your app!).
也就是说,我们可以有两个选择。一个是关闭exportSchema,另一个是在build.gradle
文件中说明开启exportSchema。
在stackoverflow中,有一个类似的问题和答案:
So if you don't need to check the schema and you want to get rid of the warning, just add exportSchema = false to your RoomDatabase, as follows.
@Database(entities = { YourEntity.class }, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
//...
}
上面是如何关闭exportSchema的方法,如果我们向开启exportSchema,那么则需要在build.gradle(:app)
中进行对应的添加,下面是一个例子
android {
compileSdk 32
defaultConfig {
applicationId "com.example.test"
minSdk 26
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
dataBinding true
viewBinding true
}
}
需要添加的是:
javaCompileOptions {
annotationProcessorOptions {
arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
在run app之后,我们会在../app/schemas/
文件夹下面看到一个json文件。比如类似下面的例子:
{
"formatVersion": 1,
"database": {
"version": 4,
"identityHash": "10dd14fb36278974582eff84a50e9e93",
"entities": [
{
"tableName": "Scan",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`scan_id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `scan_text` TEXT NOT NULL, `scan_title` TEXT NOT NULL, `date_created` INTEGER NOT NULL, `date_modified` INTEGER NOT NULL, `is_pinned` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "scanId",
"columnName": "scan_id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "scanText",
"columnName": "scan_text",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "scanTitle",
"columnName": "scan_title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "dateCreated",
"columnName": "date_created",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "dateModified",
"columnName": "date_modified",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isPinned",
"columnName": "is_pinned",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"scan_id"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "filtered_text_model",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`filtered_text_model_id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `scan_id` INTEGER NOT NULL, `type` TEXT NOT NULL, `content` TEXT NOT NULL, FOREIGN KEY(`scan_id`) REFERENCES `Scan`(`scan_id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "filteredTextModelId",
"columnName": "filtered_text_model_id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "scanId",
"columnName": "scan_id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "type",
"columnName": "type",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "content",
"columnName": "content",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"filtered_text_model_id"
],
"autoGenerate": true
},
"indices": [
{
"name": "index_filtered_text_model_scan_id",
"unique": false,
"columnNames": [
"scan_id"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_filtered_text_model_scan_id` ON `${TABLE_NAME}` (`scan_id`)"
}
],
"foreignKeys": [
{
"table": "Scan",
"onDelete": "CASCADE",
"onUpdate": "NO ACTION",
"columns": [
"scan_id"
],
"referencedColumns": [
"scan_id"
]
}
]
}
],
"views": [],
"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, '10dd14fb36278974582eff84a50e9e93')"
]
}
}