有没有一种使用 Kotlin 在 Android 中创建 Parcelable 数据类的便捷方法? [英] Is there a convenient way to create Parcelable data classes in Android with Kotlin?
问题描述
我目前在我的 Java 项目中使用了出色的 AutoParcel,它有助于创建 Parcelable 类.>
现在,我为下一个项目考虑的 Kotlin 具有数据类的概念,可自动生成 equals、hashCode 和 toString 方法.
是否有一种方便的方法可以让 Kotlin 数据类 Parcelable 以一种方便的方式(无需手动实现这些方法)?
Kotlin 1.1.4 已经发布
Android 扩展插件现在包含一个自动 Parcelable 实现生成器.在主构造函数中声明序列化属性并添加@Parcelize注解,writeToParcel()/createFromParcel()方法会自动创建:
@Parcelizeclass User(val firstName: String, val lastName: String) : Parcelable
所以你需要让他们把这个添加到你模块的build.gradle:
应用插件:'org.jetbrains.kotlin.android.extensions'安卓 {android扩展{实验 = 真}}
I'm currently using the excellent AutoParcel in my Java project, which facilitates the creation of Parcelable classes.
Now, Kotlin, which I consider for my next project, has this concept of data classes, that automatically generate the equals, hashCode and toString methods.
Is there a convenient way to make a Kotlin data class Parcelable in a convenient way (without implementing the methods manually)?
Kotlin 1.1.4 is out
Android Extensions plugin now includes an automatic Parcelable implementation generator. Declare the serialized properties in a primary constructor and add a @Parcelize annotation, and writeToParcel()/createFromParcel() methods will be created automatically:
@Parcelize
class User(val firstName: String, val lastName: String) : Parcelable
So you need to enable them adding this to you module's build.gradle:
apply plugin: 'org.jetbrains.kotlin.android.extensions'
android {
androidExtensions {
experimental = true
}
}
这篇关于有没有一种使用 Kotlin 在 Android 中创建 Parcelable 数据类的便捷方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!