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"

当使用Android Room数据库时,如果遇到'SchemaExportDirectory is not provided'警告,可以选择关闭exportSchema或者在build.gradle文件中设置导出路径。关闭方法是在RoomDatabase注解中设置exportSchema为false,或者在build.gradle中添加javaCompileOptions,并在annotationProcessorOptions中指定room.schemaLocation参数,例如设置为'$projectDir/schemas',这样运行应用后会在指定目录生成数据库Schema的json文件,用于版本管理和历史记录。

7592

被折叠的 条评论
为什么被折叠?



