当我在 Android Studio 项目中将 Kotlin 从 1.8.10 升级到 1.9.0 时,为什么会出现这么多有关 Room 的错误?

Hel*_*oCW 5 android kotlin android-studio android-room

当我使用Kotlin 1.8.10 (带有 kotlinCompilerExtensionVersion 1.4.2)Room 2.5.1时,代码 A 和代码 B 在我的 Android Studio 项目中运行良好。

将 Kotlin 从1.8.10(使用 kotlinCompilerExtensionVersion 1.4.2)升级 到1.9.0(使用 kotlinCompilerExtensionVersion '1.5.1')后,当我尝试编译项目时,出现错误 A 和错误 B。

当我用代码 AA 替换代码 A 时,错误 A 得到修复。

如何修复错误 B ?

顺便说一句,代码 C 是 Android Studio 为代码 B 自动生成的。

而且,当我将 Kotlin 从1.8.10 (with kotlinCompilerExtensionVersion 1.4.2)升级 到1.8.22 (with kotlinCompilerExtensionVersion '1.4.8')时,代码 A 和代码 B 运行良好。

代码A

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

代码B

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

错误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 {
Run Code Online (Sandbox Code Playgroud)

错误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
Run Code Online (Sandbox Code Playgroud)

代码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)
}
Run Code Online (Sandbox Code Playgroud)

代码 C——由 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);
Run Code Online (Sandbox Code Playgroud)

kph*_*hil 7

Room 需要更新其kotlinx-metadata-jvm依赖项才能读取 Kotlin 1.9+ 元数据。依赖项更新应该随 Room 一起提供2.5.3

作为参考,请参阅此处的最新评论:https://issuetracker.google.com/issues/236612358

之前的 kotlin 更新也发生了同样的事情。

  • 是的,您需要等待 Room 2.5.3 才能将 kotlin 更新到 1.9+。或者,您可以尝试使用 Room 2.6.0-alpha03,它应该与 kotlin 1.9+ 一起使用。如果您计划使用它进行生产版本,只是不建议 (2认同)