本文翻译自:Room - Schema export directory is not provided to the annotation processor so we cannot export the schema
I am using Android Database Component Room 我正在使用Android数据库组件室
I've configured everything, but when I compile, Android Studio gives me this warning: 我已经配置了所有内容,但是当我编译时,Android Studio会给我这个警告:
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. 您可以提供room.schemaLocation
注释处理器参数或将exportSchema设置为false。
As I understand it is location where DB file will be located 据我所知,它是DB文件所在的位置
How can it affect my app ? 它怎么会影响我的应用程序? What is best practice here ? 这里的最佳做法是什么? Should I use default location ( false
value) ? 我应该使用默认位置( false
值)吗?
参考:https://stackoom.com/question/2ZYdQ/Room-Schema导出目录未提供给注释处理器-因此我们无法导出架构
In the build.gradle
file for your app module, add this to the defaultConfig
section (under the android
section). 在app模块的build.gradle
文件中,将其添加到defaultConfig
部分(在android
部分下)。 This will write out the schema to a schemas
subfolder of your project folder. 这会将架构写出到项目文件夹的schemas
子文件夹。
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
Like this: 像这样:
// ...
android {
// ... (compileSdkVersion, buildToolsVersion, etc)
defaultConfig {
// ... (applicationId, miSdkVersion, etc)
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
// ... (buildTypes, compileOptions, etc)
}
// ...
As per the docs : 根据文档 :
You can set annotation processor argument (room.schemaLocation) to tell Room to export the schema into a folder. 您可以设置注释处理器参数(room.schemaLocation)以告知Room将架构导出到文件夹中。 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!). 即使它不是强制性的,最好在代码库中包含版本历史记录,然后将该文件提交到版本控制系统中(但不要随应用程序一起提供!)。
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. 因此,如果您不需要检查架构并且想要删除警告,只需将exportSchema = false
添加到RoomDatabase
,如下所示。
@Database(entities = { YourEntity.class }, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
//...
}
If you follow @mikejonesguy answer below, you will follow the good practice mentioned in the docs :). 如果您按照下面的@mikejonesguy 回答 ,您将遵循文档中提到的良好做法:)。 Basically you will get a .json
file in your ../app/schemas/
folder. 基本上,你会得到一个.json
在文件../app/schemas/
文件夹。 And it looks something like this: 它看起来像这样:
{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "53db508c5248423325bd5393a1c88c03",
"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, \"53db508c5248423325bd5393a1c88c03\")"
]
}
}
If my understanding is correct, you will get such a file with every database version update, so that you can easily follow the history of your db. 如果我的理解是正确的,您将获得每个数据库版本更新的这样一个文件,以便您可以轻松地跟踪数据库的历史记录。
Kotlin? 科特林? Here we go: 开始了:
android {
// ... (compileSdkVersion, buildToolsVersion, etc)
defaultConfig {
// ... (applicationId, miSdkVersion, etc)
kapt {
arguments {
arg("room.schemaLocation", "$projectDir/schemas".toString())
}
}
}
buildTypes {
// ... (buildTypes, compileOptions, etc)
}
}
//...
Don't forget about plugin: 不要忘记插件:
apply plugin: 'kotlin-kapt'
For more information about kotlin annotation processor please visit: Kotlin docs 有关kotlin注释处理器的更多信息,请访问: Kotlin docs
@mikejonesguy answer is perfect, just in case you plan to test room migrations (recommended), add the schema location to the source sets. @mikejonesguy 答案是完美的,以防您计划测试房间迁移(推荐),将架构位置添加到源集。
In your build.gradle file you specify a folder to place these generated schema JSON files. 在build.gradle文件中,指定一个文件夹来放置这些生成的模式JSON文件。 As you update your schema, you'll end up with several JSON files, one for every version. 在更新架构时,最终会得到几个JSON文件,每个版本一个。 Make sure you commit every generated file to source control. 确保将每个生成的文件提交给源代码管理。 The next time you increase your version number again, Room will be able to use the JSON file for testing. 下次再次增加版本号时,Room将能够使用JSON文件进行测试。
- Florina Muntenescu ( source ) Florina Muntenescu( 来源 )
build.gradle 的build.gradle
android {
// [...]
defaultConfig {
// [...]
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
// add the schema location to the source sets
// used by Room, to test migrations
sourceSets {
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
}
// [...]
}
Above answers are correct. 以上答案是正确的。 This version is easy to follow: 这个版本很容易遵循:
Because "Schema export directory is not provided to the annotation processor", So we need to provide the directory for schema export: 因为“Schema导出目录未提供给注释处理器”,所以我们需要提供模式导出的目录:
Step [1] In your file which extends the RoomDatabase, change the line to: 步骤[1]在扩展RoomDatabase的文件中,将行更改为:
`@Database(entities = ???.class,version = 1, exportSchema = true)`
Or 要么
`@Database(entities = ???.class,version = 1)`
(because the default value is always true) (因为默认值始终为true)
Step [2] In your build.gradle(project:????) file, inside the defaultConfig{ } (which is inside android{ } big section), add the javaCompileOptions{ } section, it will be like: 步骤[2]在build.gradle(project:????)文件中,在defaultConfig {} (在android {}大部分内)中,添加javaCompileOptions {}部分,它将如下:
android{
defaultConfig{
//javaComplieOptions SECTION
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation":"$projectDir/schemas".toString()]
}
}
//Other SECTION
...
}
}
$projectDir :is a variable name, you cannot change it. $ projectDir :是一个变量名,你不能改变它。 it will get your own project directory 它将获得您自己的项目目录
schemas :is a string, you can change it to any you like. schemas :是一个字符串,您可以将其更改为您喜欢的任何字符串。 For example: "$projectDir/MyOwnSchemas".toString()
例如: "$projectDir/MyOwnSchemas".toString()