问题描述
我将房间从 2.2.6
更新到 2.3.0
并在编译时开始在编译/生成的 java 代码中看到奇怪的错误.我在 .kt
文件或目录 ...build/tmp/kapt3/stubs/debug 中生成的
我只看到破坏构建的编译时错误..java
文件中没有看到任何错误/...
I updated room from 2.2.6
to 2.3.0
and started seeing weird errors in the compiled/generated java code at compile time. I do not see any errors in my .kt
files or in the generated .java
files in the directory ...build/tmp/kapt3/stubs/debug/...
I only see the compile-time error that breaks the build.
我得到的完整错误:
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
代码时,我看到它正在注释它:
When I look at the generated .java
code I see that it is annotating it:
@org.jetbrains.annotations.Nullable()
@androidx.annotation.NonNull()
private java.lang.Integer version;
我的 Kotlin 代码也有注释:
My Kotlin code is annotated as well:
import androidx.annotation.NonNull
...
@NonNull
var version: Int? = null
我的代码运行良好,经过充分测试并证明可以使用 2.2.6
房间.我更新到房间 2.3.0
后才开始遇到这个问题.
My code worked fine and is well tested and proven to work using room 2.2.6
. I only started having this problem after updating to room 2.3.0
.
旧的dependencies.gradle
Old dependencies.gradle
implementation "androidx.room:room-runtime:2.2.6"
kapt "androidx.room:room-compiler:2.2.6"
更新了dependencies.gradle
Updated dependencies.gradle
implementation "androidx.room:room-runtime:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"
感谢任何帮助,谢谢!
推荐答案
Kotlin 不使用 @NonNull
注释 - 这由您的类型本身决定,在您的情况下是可空的 Int?
.
Kotlin doesn't use @NonNull
annotations - that's determined by your type itself, which in your case is a nullable Int?
.
您需要将其更改为 Int
并为其分配一个默认值(即 -1
).
You'll need to change it to an Int
and assign it a default value (i.e., -1
).
这篇关于从房间 2.2.6 更新到 2.3.0 后,出现错误:“您必须使用 @NonNull 注释主键."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!