有人能解释一下如何迁移到newkotlin-parcelize
吗?
我试过:
- 在应用程序内部版本中将
apply plugin: 'kotlin-android-extensions'
替换为apply plugin: 'kotlin-parcelize'
.格拉德尔 - 用
import kotlinx.parcelize.Parcelize
代替import kotlinx.android.parcel.Parcelize
后者通向
Class 'ParcelZonedDateTime' is not abstract and does not implement abstract member public abstract fun describeContents(): Int defined in android.os.Parcelable
on e.g. this code:
import androidx.annotation.Keep
import kotlinx.parcelize.Parcelize
import org.threeten.bp.ZonedDateTime
@Keep
@Parcelize
data class ParcelZonedDateTime(var value: ZonedDateTime?) :Parcelable {
override fun toString(): String {
return value.toString()
}
}
So, how to migrate to the new kotlin-parcelize
?
Update:
根据 comments :是的,我在Kotlin 1.4.20上(比1.4.20-M2更新).凯泽说得对,IDE(is not abstract and does not implement abstract member public abstract fun describeContents())中的错误可以而且必须被忽略.但我在使用泛型时遇到了一个问题:
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
sealed class MyDialogEvent {
@Parcelize
data class Confirmed<T: Parcelable>(val identity: String, val data: T) : Parcelable
}
生成的代码是
@kotlin.Metadata(mv = {1, 4, 1}, bv = {1, 0, 3}, k = 3)
public static final class Creator implements android.os.Parcelable.Creator<com.example.stackoverflow.MyDialogEvent.Confirmed> {
public Creator() {
super();
}
@org.jetbrains.annotations.NotNull()
@java.lang.Override()
public final com.example.stackoverflow.MyDialogEvent.Confirmed<T>[] newArray(int size) {
return null;
}
@org.jetbrains.annotations.NotNull()
@java.lang.Override()
public final com.example.stackoverflow.MyDialogEvent.Confirmed<T> createFromParcel(@org.jetbrains.annotations.NotNull()
android.os.Parcel in) {
return null;
}
}
and during compilation I get this error for it (line numbers won't match the sample code, but you can easily identify the matching lines by comparing the method names):
MyDialogEvent.java:167: error: non-static type variable T cannot be referenced from a static context
public final com.example.stackoverflow.MyDialogEvent.Confirmed<T>[] newArray(int size) {
MyDialogEvent.java:173: error: non-static type variable T cannot be referenced from a static context
public final com.example.stackoverflow.MyDialogEvent.Confirmed<T> createFromParcel(@org.jetbrains.annotations.NotNull()