1

Working my first android app, kotlin 1.3.50. I initially implemented Parcelable, but switched to parcelize and then added the enums. Here is my data class

package com.igniterobotics.scouting_2019.Models

import android.os.Parcel
import android.os.Parcelable
import com.igniterobotics.scouting_2019.Enums.Movement
import com.igniterobotics.scouting_2019.Enums.Preload
import com.igniterobotics.scouting_2019.Enums.StartingPosition

@Parcelize
data class AutonResult(var hatchCount: Int, var cargoCount: Int, 
      var intakeDrop: Int, var itemDrops: Int, var startingPosision: StartingPosition, 
      var preload: Preload, var movement: Movement
)

One question mentioned plugin order in the build.gradle as a source for the issue. Here's mine

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

Any idea why I @Parcelize can't be resolved?

Michiel Leegwater
  • 1,172
  • 4
  • 11
  • 27
BCarlson
  • 1,122
  • 2
  • 11
  • 26
  • You are missing the experimental flag https://kotlinlang.org/docs/tutorials/android-plugin.html#enabling-experimental-features – EpicPandaForce Oct 15 '19 at 12:51

3 Answers3

3
android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
       ....
    }
    buildTypes {
        release {
            ...
        }
    }

    androidExtensions {
        experimental = true
    }
}
Dariusz Bacinski
  • 8,324
  • 9
  • 38
  • 47
Hinal Halvadia
  • 236
  • 2
  • 6
  • Thanks. I'll test this in a couple of hours. This is a personal project I'm working on. – BCarlson Oct 15 '19 at 14:13
  • I had added the androidExtensions at the root level and not a child of android node. Additionall, my data class didn't have : Parcelable at the end. – BCarlson Oct 15 '19 at 18:22
1

You need to add

androidExtensions {
        experimental = true
    }

In your app's build.gradle

r2rek
  • 2,083
  • 13
  • 16
  • I tried adding it after the plugins, Is that in the correct location? I haven't worked with gradle files much, since I most do C# development. If that's the correct place, maybe I had a typo or something. – BCarlson Oct 15 '19 at 13:09
  • Add it inside the android{...} Tag – r2rek Oct 15 '19 at 14:53
1

If you're still facing this issue in 2021 you can follow this

Don’t forget that Parcelize feature in Kotlin is part of the kotlin-android-extensions compiler plugin, so removing the plugin will end up making all your Parcelable classes not compiling if they depend on the Parcelize annotation. JetBrains extracted the Parcelize from Kotlin Android Extensions to a new plugin,

kotlin-parcelize

First you will need to add kotlin-parcelize plugin to your module.

plugins {
    ..
    id 'kotlin-parcelize'
}

Then change your old import statement from

import kotlinx.android.parcel.Parcelize

to

import kotlinx.parcelize.Parcelize

Example

    import kotlinx.parcelize.Parcelize
    import android.os.Parcelable

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

more

Chidi
  • 189
  • 2
  • 10