vepzfe
vepzfe

Reputation: 4527

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?

Upvotes: 133

Views: 117827

Answers (6)

Giovannini Barbosa
Giovannini Barbosa

Reputation: 71

Before anything you should bump your dependencies versions. After that, you should change some imports to dismiss the warning and use the right way.

In my case (using macOS), just press Command + Shift + R

Find this: import kotlinx.android.parcel.Parcelize

Replace to: import kotlinx.parcelize.Parcelize

Upvotes: 3

Sibtain Raza
Sibtain Raza

Reputation: 213

  1. kotlin-android-extensions is deprecated so replace it from kotlin-parcelize
  2. 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

Upvotes: 6

All Іѕ Vаиітy
All Іѕ Vаиітy

Reputation: 26402

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

Upvotes: 15

Yvgen
Yvgen

Reputation: 2212

Just update your import

from: import kotlinx.android.parcel.Parcelize

to the: import kotlinx.parcelize.Parcelize

Upvotes: 0

G00fY
G00fY

Reputation: 5277

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

Upvotes: 246

A. Patrik
A. Patrik

Reputation: 1692

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

Upvotes: 65

Related Questions