Search code examples
androidkotlinandroid-studioandroid-room

Why do I get so many errors about Room when I upgrade Kotlin from 1.8.10 to 1.9.0 in an Android Studio project?


The Code A and Code B work well in my Android Studio project when I use Kotlin 1.8.10 (with kotlinCompilerExtensionVersion 1.4.2) and Room 2.5.1.

After I upgrade Kotlin from 1.8.10 (with kotlinCompilerExtensionVersion 1.4.2) to 1.9.0 (with kotlinCompilerExtensionVersion '1.5.1'), I get the Error A and the Error B when I try to compile the project.

The Error A is fixed when I replace the Code A with Code AA.

How can I fix the Error B ?

BTW, the Code C is the generated automatically by Android Studio for the Code B.

And more, the Code A and Code B work well when I upgrade Kotlin from 1.8.10 (with kotlinCompilerExtensionVersion 1.4.2) to 1.8.22 (with kotlinCompilerExtensionVersion '1.4.8').

Code A

@Entity(tableName = "info_table", indices = [Index("createdDate")])
data class RecordEntity(
    @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") var id: Int = 0,
     var text:     String = "", 
         ...
)

Code B

@Dao
interface  RecordDao {
    @Query("SELECT * FROM info_table where id=:id")
    suspend fun getByID(id:Int): RecordEntity    
    ...
}

Error A

There are multiple good constructors and Room will pick the no-arg constructor. You can use the @Ignore annotation to eliminate unwanted constructors. public final class RecordEntity {

Error B

Not sure how to convert a Cursor to this method's return type (java.lang.Object).    public abstract java.lang.Object getByID(int id, @org.jetbrains.annotations.NotNull
Unused parameter: $completion    public abstract java.lang.Object getByID(int id, @org.jetbrains.annotations.NotNull

Code AA

@Entity(tableName = "info_table", indices = [Index("createdDate")])
data class  RecordEntity(
     @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") var id: Int = 0,
     var text:    String = "",  
       ...
) {
    @Ignore
     constructor() : this(0)
}

Code C -- Generated automatically by Android Studio

@androidx.room.Query(value = "SELECT * FROM info_table where id=:id")
@org.jetbrains.annotations.Nullable
public abstract java.lang.Object getByID(int id, @org.jetbrains.annotations.NotNull
kotlin.coroutines.Continuation<? super com.hicalc.soundrecorder.data.database.RecordEntity> $completion);

Solution

  • Room needs to update its kotlinx-metadata-jvm dependency to be able to read Kotlin 1.9+ metadata. Dependency update should come with Room 2.5.3.

    For reference see last comments here: https://issuetracker.google.com/issues/236612358

    Same thing happened with previous kotlin updates as well.