我将room从2.2.6
更新为2.3.0
,并在编译时开始在编译/生成的java代码中看到奇怪的错误。我在.kt
文件或目录...build/tmp/kapt3/stubs/debug/...
中生成的.java
文件中没有看到任何错误,我只看到破坏构建的编译时错误。
我得到的完整错误:
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
我的代码运行良好,经过良好的测试并证明可以使用2.2.6
室工作。我是在更新到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
注释——这是由您的类型本身决定的,在您的情况下,它是一个可为null的Int?
。
您需要将其更改为Int
,并为其指定一个默认值(即-1
(。