首页 > 解决方案 > 从房间 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"

任何帮助表示感谢!

标签: androidsqlitekotlinandroid-roomandroid-jetpack

解决方案


Kotlin 不使用@NonNull注释 - 这由您的类型本身决定,在您的情况下是 nullable Int?

您需要将其更改为 anInt并为其分配一个默认值(即-1)。


推荐阅读