是否有使用Kotlin在Android中创建Parcelable数据类的便捷方法? [英] Is there a convenient way to create Parcelable data classes in Android with Kotlin?
问题描述
我目前在我的Java项目中使用了出色的 AutoParcel ,它有助于创建Parcelable类.
I'm currently using the excellent AutoParcel in my Java project, which facilitates the creation of Parcelable classes.
现在,我在下一个项目中考虑的Kotlin具有这种数据类的概念,它可以自动生成equals,hashCode和toString方法.
Now, Kotlin, which I consider for my next project, has this concept of data classes, that automatically generate the equals, hashCode and toString methods.
是否有一种便捷的方法可以使Kotlin数据类可打包(无需手动实现方法)?
Is there a convenient way to make a Kotlin data class Parcelable in a convenient way (without implementing the methods manually)?
推荐答案
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
因此,您需要允许他们将其添加到模块的 build.gradle :
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屋!