Kotlin升级1.5.20 Room不能导出schemas

在 Kotlin 从1.5.10 升级到 1.5.20 时发现,Room不能导出 schemas 了,并且出现如下报错:

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.
public abstract class MyDatabase extends androidx.room.RoomDatabase {

这里提示的很清楚了,说是没有设置 schema 的导出目录。
可以设置 room.schemaLocation 注解处理器参数,也可以将 exportSchema 设置为 false。

不过项目中,一直是按照 Google文档 中的要求配置了 room.schemaLocation

我的项目代码:

android {
    compileSdkVersion 30

    defaultConfig {
        applicationId "xxx"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        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'
    }
}

经过测试,将 Kotlin 版本降为 1.5.10,schemas 可以正常导出,由此我猜测,应该是在 Kotlin 版本升级中,注解处理器的语法有了变动。

果不其然,在 Kotlin 的文档 中看到:使用 arguments {} 将参数传递给注解处理器:

kapt {
    arguments {
        arg("key", "value")
    }
}

按照上面的格式重写修改:

android {
    ...
        kapt {
            arguments {
                arg("room.schemaLocation", "$projectDir/schemas".toString())
            }
        }
    }
    ...
}

再次build,成功导出 schemas 文件。

最后再吐槽下 Google 文档更新的太不及时了,毕竟 Kotlin 1.5.20 正式版出来这么久了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章