1

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.

The full error I get:

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;

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;

My Kotlin code is annotated as well:

import androidx.annotation.NonNull
...
    @NonNull
    var version: Int? = null

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.

Old dependencies.gradle

implementation "androidx.room:room-runtime:2.2.6"
kapt "androidx.room:room-compiler:2.2.6"

Updated dependencies.gradle

implementation "androidx.room:room-runtime:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"

Any help is appreciated thanks!

Tim
  • 1,606
  • 2
  • 21
  • 42
  • 1
    because this : `var version: Int? = null` can be null, in java sphere it will be converted to a `Integer` object and not evaluated as a primitive `int`. The error is quite explicit, primary keys should not be `null` - as a quick fix just make it `var int = -1`. However consider creating the object with a matching constructor as a data class. – Mark May 03 '21 at 21:53

1 Answers1

4

Kotlin doesn't use @NonNull annotations - that's determined by your type itself, which in your case is a nullable Int?.

You'll need to change it to an Int and assign it a default value (i.e., -1).

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Thanks, @ianhanniballake! Now that you point that out it makes a lot of sense. I wonder why it worked in the prior version of room? It has not had any problems the way it was before the update. – Tim May 04 '21 at 02:06