115

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

Boken
  • 4,825
  • 10
  • 32
  • 42
vepzfe
  • 4,217
  • 5
  • 26
  • 46

6 Answers6

223

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
G00fY
  • 4,545
  • 1
  • 22
  • 26
  • 1
    Step 2 was missing from other descriptions and fixed my issue, thanks. – Veener Nov 26 '20 at 16:40
  • I'm new to android development and have no idea but `kotlinx` doesn't have `parcelize` submodule(?). – Hiro Feb 20 '21 at 17:04
  • What's the `project build.gradle` part for? Doesn't seem to be necessary – Florian Walther Mar 10 '21 at 10:44
  • @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 Mar 10 '21 at 14:02
  • @G00fY Thank you for the clarification. I have the `kotlin-android` plugin which is added to a new project by default. – Florian Walther Mar 10 '21 at 14:22
  • even after removing, apply plugin: 'kotlin-android-extensions' Can still see package kotlinx.android.parcel.Parcelize being able to be used. So gets confused with import kotlinx.parcelize.Parcelize Do I need to remove something else as well ? – Karan Sharma Aug 17 '21 at 06:25
  • I wrote an article about this to use custom @TypeParceler: https://medium.com/@chrisathanas/how-to-use-parcels-on-kotlin-multiplatform-mobile-kmm-e29590816624 – RealityExpander Oct 15 '22 at 21:30
60

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

A. Patrik
  • 1,530
  • 9
  • 20
  • 1
    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? – vepzfe Nov 20 '20 at 07:17
  • 1
    found the answer. Its not available in the current kotlin stable version yet - https://medium.com/@drjacky/where-should-we-add-plugins-b85517b8a62f – vepzfe Nov 20 '20 at 09:16
  • @G00fY Both 'kotlin-parcelize' and 'org.jetbrains.kotlin.plugin.parcelize' are working for me. – vepzfe Nov 23 '20 at 19:05
  • what about this line : classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" – Vikas Pandey Dec 28 '20 at 12:52
  • Please add that the `kotlin-parcelize` plugin is separated only after kotlin version 1.4.20 and won't be found below it. – guneetgstar May 21 '21 at 12:06
  • I am having 2 projects, one is working perfact with what i have, like AndroidStudio 3.4, in project build (Gradle Version-3.4.1, ext.kotlin_version = '1.3.61') IDE kotlin plugin version is 1.3.61. ONe of the project is throwing error as "kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.16." Can u please suggest any solution. Im been facing from last 2-3 weeks. – Mihir Lakhia Jul 27 '21 at 23:34
13

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

All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
5
  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
Sibtain Raza
  • 205
  • 2
  • 4
1

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

Nice, now it's right!

0

Just update your import

from: import kotlinx.android.parcel.Parcelize

to the: import kotlinx.parcelize.Parcelize

Yvgen
  • 2,036
  • 2
  • 23
  • 27