是否有使用Kotlin在Android中创建Parcelable数据类的便捷方法? [英] Is there a convenient way to create Parcelable data classes in Android with Kotlin?

查看:490
本文介绍了是否有使用Kotlin在Android中创建Parcelable数据类的便捷方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的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扩展插件现在包括一个自动Parcelable实现生成器.在主构造函数中声明序列化的属性并添加@Parcelize批注,将自动创建writeToParcel()/createFromParcel()方法:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆