简体   繁体   中英

Kotlin 1.3.50 Parcelize unresolved reference error

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?

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

    androidExtensions {
        experimental = true
    }
}

You need to add

androidExtensions {
        experimental = true
    }

In your app's build.gradle

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM