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);
2
Answers
Room needs to update its
kotlinx-metadata-jvm
dependency to be able to read Kotlin 1.9+ metadata. Dependency update should come with Room2.5.3
.For reference see last comments here: https://issuetracker.google.com/issues/236612358
Same thing happened with previous kotlin updates as well.
You can also solve the problem by changing to
KSP
fromKAPT
while Room support is still in progress for Kotlin1.9+
Also, it’s recommended to migrate to KSP if your libraries support KSP. You will not face this kind of problem if you migrate to KSP and also it’s faster than KAPT.
See here for migration steps.