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

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

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"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小超同学你好

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

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

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

打赏作者

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

抵扣说明:

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

余额充值