错误:未向注解处理器提供架构导出目录,因此无法导出架构。您可以提供 `room.schemaLocation` 注解处理器参数,或将 exportSchema 设置为 false。
error: 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.
ID: android/room-schema-export-missing
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Room 2.5 - 2.6 | active | — | — | — |
| Android Gradle Plugin 7.4 - 8.2 | active | — | — | — |
根因分析
Room 数据库注解处理器需要架构导出目录以进行迁移验证,但未在 build.gradle 中配置,或者 exportSchema 设置为 true 但未提供路径。
English
Room database annotation processor requires a schema export directory for migration validation, but it is not configured in build.gradle or exportSchema is set to true without a path.
官方文档
https://developer.android.com/training/data-storage/room/migrating#export-schema解决方案
-
Add the following to build.gradle (app-level) for kapt: 'kapt { arguments { arg("room.schemaLocation", "$projectDir/schemas") } }' or for annotationProcessor: 'defaultConfig { javaCompileOptions { annotationProcessorOptions { arguments = ["room.schemaLocation": "$projectDir/schemas"] } } }' -
Set exportSchema = false in the @Database annotation if migration validation is not needed: '@Database(entities = [User::class], version = 1, exportSchema = false)'
无效尝试
常见但无效的做法:
-
50% 失败
If exportSchema is false, the schema is not exported, but the error may still occur if the processor expects the argument; the build.gradle configuration is still needed.
-
50% 失败
The path must be a valid project-relative directory; a wrong path causes the processor to fail silently or create the schema in an unexpected location.