与gson的Kotlin parcelize问题
我正在使用@parcelize for gson
这是我的class
@Parcelize data class CommunityModel(@SerializedName("post") val post: PostModel, @SerializedName("is_liked") val isLiked: Boolean, @SerializedName("post_like") val postLike: QuestionModel, @SerializedName("polling_options") val pollingOptions: List, @SerializedName("post_polled") val postPolled: Boolean) : Parcelable
我得到错误Unable to invoke no-args constructor for class. Register an InstanceCreator with Gson for this type may fix this problem.
Unable to invoke no-args constructor for class. Register an InstanceCreator with Gson for this type may fix this problem.
。
但是这个错误只出现在较旧的Android版本 (低于5.0)
我试着实现默认的构造函数:
constructor: this(PostModel(), true, QuestionModel(), emptyList(), true)
但它给了我java.lang.VerifyError
我正在使用rofjava2和gson转换器版本2.3的 retrofit2
我的kotlin版本是1.1.51
它知道错误吗? 还是我做错了什么?
无参数编译器插件
no-arg编译器插件为具有特定注释的类生成额外的零参数构造函数。
生成的构造函数是合成的,所以不能直接从Java或Kotlin调用,但可以使用reflection来调用它。
这允许Java持久性API(JPA)实例化数据类,虽然它没有从Kotlin或Java的角度来看零参数构造函数(参见下面的kotlin-jpa插件的描述)。
在Gradle中使用
这个用法和全部打开很相似。
添加插件并指定必须导致为注释类生成无参数构造函数的注释列表。
buildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version" } } apply plugin: "kotlin-noarg"
来源https://kotlinlang.org/docs/reference/compiler-plugins.html