Kotlin 23. Kotlin Room问题:Schema export directory is not provided to the annotation processor

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')"
    ]
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

破浪会有时

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值