android - 从房间 2.2.6 更新到 2.3.0 后,我收到一个错误:“您必须使用 @NonNull 注释主键。”
问题描述
2.2.6
我从to更新了房间,2.3.0
并在编译时开始在编译/生成的 java 代码中看到奇怪的错误。我在我的.kt
文件或.java
目录中生成的文件中看不到任何错误,...build/tmp/kapt3/stubs/debug/...
我只看到破坏构建的编译时错误。
我得到的完整错误:
error: You must annotate primary keys with @NonNull. "version" is nullable. SQLite considers this a bug and Room does not allow it. See SQLite docs for details: https://www.sqlite.org/lang_createtable.html
private java.lang.Integer version;
当我查看生成的.java
代码时,我看到它正在注释它:
@org.jetbrains.annotations.Nullable()
@androidx.annotation.NonNull()
private java.lang.Integer version;
我的 Kotlin 代码也有注释:
import androidx.annotation.NonNull
...
@NonNull
var version: Int? = null
我的代码运行良好,并且经过良好测试并证明可以使用 room 2.2.6
。我只是在更新到 room 后才开始遇到这个问题2.3.0
。
旧的dependencies.gradle
implementation "androidx.room:room-runtime:2.2.6"
kapt "androidx.room:room-compiler:2.2.6"
更新了 dependencies.gradle
implementation "androidx.room:room-runtime:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"
任何帮助表示感谢!
解决方案
Kotlin 不使用@NonNull
注释 - 这由您的类型本身决定,在您的情况下是 nullable Int?
。
您需要将其更改为 anInt
并为其分配一个默认值(即-1
)。
推荐阅读
- java - 如何使用 Gradle 复制 java 包
- gremlin - 如何获得独立于内部结构的子图?
- vba - 将word文档中的所有文本复制到另一个word文档而不格式化
- google-cloud-ml - CPU/GPU 缩放日志
- reactjs - 是否可以将相机拍摄的图像直接上传到 Firebase 存储?
- laravel - 仅仅为了避免 REST 反模式而为基本相同的逻辑定义两个单独的 API 端点?
- hibernate - 向 JPA 中的 ManyToMany 集合添加条目时如何避免不必要的查询?
- c++ - 在不添加 MOV* 指令的情况下实现“易失性”的效果?
- php - Woocommerce 多供应商拆分运输,无需插件或自定义插件
- javascript - Veux 存储中未定义数组