Home > Net >  How to use Parcelize Annotation in Android Kotlin?
How to use Parcelize Annotation in Android Kotlin?

Time:12-16

I am following https://developer.android.com/jetpack/compose/state#parcelize to make a Parcelable data class with Kotlin in Jetpack Compose, however the @Parcelize annotation is not available to be imported.

I can add the block below to my code, and can import Parcelable fine, but not Parcelize:

@Parcelize
data class City(val name: String, val country: String) : Parcelable

I read https://stackoverflow.com/a/64948848/11162441 and added id 'org.jetbrains.kotlin.plugin.parcelize' version '1.7.22' apply false to my project-level Gradle build and synced, however I am still not able to import the annotation. I am using Android Kotlin version 1.7.10.

If the Parcelize annotation is out-dated, I want to use the latest annotation.

CodePudding user response:

That looks correct. Make sure you are using these imports for the @Parelize

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

Maybe you have your Gradle files misconfigured. Make sure that in your app build.gradle you import the plugin like this:

plugins {
   id 'org.jetbrains.kotlin.plugin.parcelize' version "1.7.20"
}

I don't know about 1.7.22, but this is the version I've been using. If you do a File >> Sync Gradle Files on the Android Studio, do you get any errors?

Please make sure that you are setting the plugin in your application module Gradle file, along with your dependencies, you should not apply these plugins in the project grade file.

CodePudding user response:

In your module-level build.gradle file you should have the following:

plugins {
id 'kotlin-parcelize'
}

I never imported parcelize into the project-level gradle file, try this.

  • Related