15

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?

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Noam Silverstein
  • 469
  • 1
  • 8
  • 16

3 Answers3

26

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.

Community
  • 1
  • 1
Some random IT boy
  • 2,687
  • 2
  • 11
  • 28
  • (After 19/11/2019 update) - Problem persists for me in 3.5.2 AS + plugin and 1.3.60 kotlin – Breadbin Nov 19 '19 at 17:22
  • For me it works; Android Studio 3.5.2 Kotlin plugin version: 1.3.60-release-Studio3.5.1 (Stable update channel) Android Studio 3.5.2 Build #AI-191.8026.42.35.5977832, built on October 30, 2019 JRE: 1.8.0_202-release-1483-b49-5587405 x86_64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o macOS 10.14.6 – Some random IT boy Nov 21 '19 at 07:49
  • Did all the usual to rule out any caching. Came back to it after lunch and it was fine. Time heals all wounds it seems – Breadbin Nov 29 '19 at 09:56
  • Problem persists for me too in Android Studio version 4.1.3 and Kotlin Version 1.4.32. – Namrata Bagerwal Mar 30 '21 at 13:34
4

In my scenario, I have already using the Kotlin plugin version latest than 1.3.40 version.

But I still got the Android Studio Error, compile success, but IDE complain about the @Parcelize annotation.

Maybe you got this aswell.

Here is the solution.

Check your kotlin plugin version which you are configured in Gradle is the same version which bundled in Android Studio.

I got the error because my Gradle koltin plugin version is 1.3.61 and the Android Studio bundled version is 1.3.50

enter image description here

enter image description here

How to upgrade the Kotlin plugin version?

Tools -> Kotlin -> Configure kotlin plugin update

check the latest version and install, keep the two versions are the same.

enter image description here

Jeffery Ma
  • 2,117
  • 16
  • 20
0

You can always just implement Parcelable(cmd+enter/alt+enter on class name -> `Add Parcelable Implementation" in order for IDE to create method schemas for you) if you dont' want to/can't use experimental extensions.

I've, however, been using @Parcelize in prod environment(after a lot of testing though) for over a year, with no issues.(Just an opinion though, not saying you should)

r2rek
  • 1,538
  • 7
  • 12