有人能解释一下如何迁移到newkotlin-parcelize吗?

我试过:

  1. 在应用程序内部版本中将apply plugin: 'kotlin-android-extensions'替换为apply plugin: 'kotlin-parcelize'.格拉德尔
  2. 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()

推荐答案

That's a bug in kapt and it's reported here. Actually the java code you posted is a stub class which generated by kapt to run annotation processors on it (as you can see, there is no real implementation, just the structure and annotations). So as a workaround you can move the code with @Parcelize annotations to a separate module where you don't have kapt enabled. kotlin-parcelize itself does not use annotation processors to generate code, it uses a compiler plugin to emit the IR directly I believe.

Update: the issue is fixed in kotlin 1.5

Kotlin相关问答推荐

Kotlin—列出具有不同T的列表之间的操作'

用A*搜索算法解决特修斯和米诺陶尔难题

在Kotlin中,我是否可以访问已知的WHEN子句值?

为何Kotlin标准库中的AND和OR函数不能像&&和||一样进行短路运算?

根据字符串值动态过滤数组列表 - kotlin

验证构造函数中的值组合

Swift vs Kotlin 在排序数组上的表现

Koin Android:org.koin.error.NoBeanDefFoundException

Kotlin:泛型、反射以及类型 T 和 T:Any 之间的区别

Android Kotlin StringRes 数量String

如何在 Kotlin 中传递有界通配符类型参数?

面临一些未知问题一些后端jvm内部错误

项目未与 Gradle 链接

可以在函数参数中使用解构吗?

如何在Kotlin中将字符串转换为InputStream?

内联 Kotlin 方法没有覆盖报告

项目不会使用 Kotlin 1.1.3 构建

从 java 活动 *.java 启动 kotlin 活动 *.kt?

Kotlin中默认导入哪些包/函数?

对有延迟的 Rxjava 可观察对象进行单元测试