ChatGPT解决这个技术问题 Extra ChatGPT

How to use @Parcelize now that kotlin-android-extensions is being deprecated?

How do I replace annotation class Parcelize from package kotlinx.android.parcel with Parcelize which is not coming from the kotlin-android-extensions plugin?


G
G00fY

This should be the new plugin: https://plugins.gradle.org/plugin/org.jetbrains.kotlin.plugin.parcelize

If using Plugins DSL you can use the plugin ID in submodules. Make sure that the latest Kotlin Android plugin is available from the project's classpath.

// project build.gradle
plugins {
    ..
    id "org.jetbrains.kotlin.android" version "1.4.20" apply false
}

// app build.gradle
plugins {
    ..
    id 'kotlin-parcelize'
}

When using kts you can write ->

// project build.gradle.kts
plugins {
    ..
    kotlin("android") version "1.4.20" apply false
}

// app build.gradle.kts
plugins {
    ..
    id("kotlin-parcelize")
}

--- OR Legacy Plugin Application ---

Step 1. Update to latest kotlin version - 1.4.20 and replace

apply plugin: 'kotlin-android-extensions'

with this ->

apply plugin: 'kotlin-parcelize'

Step 2. Remove this code from the android {}

androidExtensions {
    experimental = true
}

Step 3. Finally, replace old import ->

import kotlinx.android.parcel.Parcelize

with new import

import kotlinx.parcelize.Parcelize

Step 2 was missing from other descriptions and fixed my issue, thanks.
I'm new to android development and have no idea but kotlinx doesn't have parcelize submodule(?).
What's the project build.gradle part for? Doesn't seem to be necessary
@FlorianWalther the "kotlin-parcelize" identifier is part of the Kotlin Android Plugin. So you need to have the Kotlin Android Plugin in your classpath. Maybe you have added it with the old classpath syntax? Or you have a single Android module (app Module) and added all plugins only there. Then you don't need to define it in your project build.gradle gradle.
@G00fY Thank you for the clarification. I have the kotlin-android plugin which is added to a new project by default.
A
A. Patrik

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

Edit (source): https://proandroiddev.com/migrating-the-deprecated-kotlin-android-extensions-compiler-plugin-to-viewbinding-d234c691dec7


Thanks for the answer. Where exactly is this plugins {} code block supposed to go in? in the project level gradle file or app level gradle file?
found the answer. Its not available in the current kotlin stable version yet - medium.com/@drjacky/where-should-we-add-plugins-b85517b8a62f
@G00fY Both 'kotlin-parcelize' and 'org.jetbrains.kotlin.plugin.parcelize' are working for me.
what about this line : classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
Please add that the kotlin-parcelize plugin is separated only after kotlin version 1.4.20 and won't be found below it.
A
All Іѕ Vаиітy

With Groovy

build.gradle (project level)

dependencies {
    // .... project level dependencies
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files

    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20-RC"
}

build.gradle (app level)

plugins {
    ...
    id 'kotlin-parcelize'
}

and the data class

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

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

you can find the latest version here https://plugins.gradle.org/plugin/org.jetbrains.kotlin.plugin.parcelize


S
Sibtain Raza

kotlin-android-extensions is deprecated so replace it from kotlin-parcelize If there is @Parcelize annotation used for model classes so you have to replace its import from kotlinx.android.parcel.Parcelize to import kotlinx.parcelize.Parcelize


Y
Yvgen

Just update your import

from: import kotlinx.android.parcel.Parcelize

to the: import kotlinx.parcelize.Parcelize