androidparcelablekotlin-android-extensionsandroid-bundlekotlinx.serialization

Class X is not abstract and does not implement fun writeToParcel() defined in android.os.Parcelable


In my Android app, I want to add a Bundle including a Place object described below to my Intent. Since serializable was slow and not recommended, I preferred Parcelable.

Althoug I use Kotlin 1.3.31, I have problems parcelizing some data classes. Example:

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
data class Place(val street: String, val postal: String, val city: String) : Parcelable

and Android Studio complains:

Class 'Place' is not abstract and does not implement abstract member public abstract fun writeToParcel(p0: Parcel!, p1: Int): Unit defined in android.os.Parcelable

According to some tutorials

That’s it! You don’t need to write any parcel methods anymore!

https://android.jlelse.eu/yet-another-awesome-kotlin-feature-parcelize-5439718ba220

and I do not want to use

androidExtensions {
    experimental = true
}

in production stuff.

What alternatives would I have here?


Solution

  • UPDATE 19/11/2019

    After the stable release of Kotlin 1.3.60 and its corresponding Android Studio plugin the issue is no more. Let's celebrate

    UPDATE 27/08/2019

    After a bit of more researching and testing with the brand new Kotlin 1.3.50 seems that the issue is going to be finally fully addressed when they release Kotlin 1.3.60 as per this YouTrack issue

    EDIT 19/06/2019

    With Kotlin 1.3.40 release the @Parcelize annotation is out of experimental and works quite nicely. The only issue is a reported issue that makes the IDE go red, leaving this to a side the code does compile and run perfectly.

    Sample code

    Sample code

    Sample code

    I have also tested with this kind of objects and it does also work:

    enter image description here

    Old answer

    I'm facing the exact same issue and while investigating I found this:

    Jake Wharton on parcelize

    So the looks like the @Parcelize annotation will be fully stable starting from Kotlin 1.3.40. Until then you will have to set the experimental flag. (Sadly)


    They have wrongly pushed @Parcelize outside experimental features and you still get that compilation error.