Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a convenient way to create Parcelable data classes in Android with Kotlin?

People also ask

How do I create a Parcelable data class Kotlin?

There are 3 ways you can make your class Parcelable: Implementing the Parcelable interface and defining the serialization yourself (The traditional way) Using Android Studio plugins, like Android Parcelable code generator. Using annotation-based libraries, like Parceler.

How do I make a Parcelable Android model?

Create Parcelable class without plugin in Android Studioimplements Parcelable in your class and then put cursor on "implements Parcelable" and hit Alt+Enter and select Add Parcelable implementation (see image). that's it.

Why Parcelable is faster than serializable?

Parcel able is faster than serializable. Parcel able is going to convert object to byte stream and pass the data between two activities. Writing parcel able code is little bit complex compare to serialization. It doesn't create more temp objects while passing the data between two activities.


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
    }
}

You can try this plugin:

android-parcelable-intellij-plugin-kotlin

It help you generate Android Parcelable boilerplate code for kotlin's data class. And it finally look like this:

data class Model(var test1: Int, var test2: Int): Parcelable {

    constructor(source: Parcel): this(source.readInt(), source.readInt())

    override fun describeContents(): Int {
        return 0
    }

    override fun writeToParcel(dest: Parcel?, flags: Int) {
        dest?.writeInt(this.test1)
        dest?.writeInt(this.test2)
    }

    companion object {
        @JvmField final val CREATOR: Parcelable.Creator<Model> = object : Parcelable.Creator<Model> {
            override fun createFromParcel(source: Parcel): Model{
                return Model(source)
            }

            override fun newArray(size: Int): Array<Model?> {
                return arrayOfNulls(size)
            }
        }
    }
}

Just click on the data keyword of your kotlin data class, then press alt+Enter, select the first option saying "Add Parceable Implementation"


Have you tried PaperParcel? It's an annotation processor which automatically generates the Android Parcelable boilerplate code for you.

Usage:

Annotate your data class with @PaperParcel, implement PaperParcelable, and add a JVM static instance of the generated CREATOR e.g.:

@PaperParcel
data class Example(
  val test: Int,
  ...
) : PaperParcelable {
  companion object {
    @JvmField val CREATOR = PaperParcelExample.CREATOR
  }
}

Now your data class is Parcelable and can be passed directly to a Bundle or Intent

Edit: Update with latest API


The best way with no boilerplate code at all is Smuggler gradle plugin. All you need is just implement AutoParcelable interface like

data class Person(val name:String, val age:Int): AutoParcelable

And that's all. Works for sealed classes as well. Also this plugin provides compile time validation for all AutoParcelable classes.

UPD 17.08.2017 Now with Kotlin 1.1.4 and Kotlin Android extensions plugin you could use @Parcelize annotation. In this case, the example above will look like:

@Parcelize class Person(val name:String, val age:Int): Parcelable

No need for data modifier. The biggest downside, for now, is using kotlin-android-extensions plugin which has a lot of other functions that could be unnecessary.