问题描述
我正在使用 Android 数据库组件室
I am using Android Database Component Room
我已经配置了一切,但是当我编译时,Android Studio 给了我这个警告:
I've configured everything, but when I compile, Android Studio gives me this warning:
模式导出目录没有提供给注解处理器,所以我们无法导出架构.您可以提供room.schemaLocation
注释处理器参数或设置exportSchema 为 false.
据我所知,这是数据库文件所在的位置
As I understand it is the location where DB file will be located
它如何影响我的应用程序?这里的最佳做法是什么?我应该使用默认位置(false
值)吗?
How can it affect my app? What is the best practice here? Should I use the default location (false
value)?
推荐答案
根据 文档:
您可以设置注释处理器参数 (room.schemaLocation) 来告诉 Room 将架构导出到文件夹中.尽管这不是强制性的,但在您的代码库中包含版本历史记录是一个很好的做法,您应该将该文件提交到您的版本控制系统中(但不要随您的应用一起提供!).
因此,如果您不需要检查架构并且想要摆脱警告,只需将 exportSchema = false
添加到您的 RoomDatabase
中,如下所示.
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 {
//...
}
如果您遵循下面的@mikejonesguy answer,您将遵循文档中提到的良好做法:).基本上,您将在 ../app/schemas/
文件夹中获得一个 .json
文件.它看起来像这样:
If you follow @mikejonesguy answer below, you will follow the good practice mentioned in the docs :).Basically you will get a .json
file in your ../app/schemas/
folder.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.
这篇关于Room - 架构导出目录未提供给注释处理器,因此我们无法导出架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!