How do I replace annotation class Parcelize
from package kotlinx.android.parcel
with @Parcelize
which is not coming from the kotlin-android-extensions
plugin?
6 Answers
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
-
1Step 2 was missing from other descriptions and fixed my issue, thanks.– VeenerNov 26, 2020 at 16:40
-
I'm new to android development and have no idea but
kotlinx
doesn't haveparcelize
submodule(?).– HiroFeb 20, 2021 at 17:04 -
What's the
project build.gradle
part for? Doesn't seem to be necessary Mar 10, 2021 at 10:44 -
1@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.– G00fYMar 10, 2021 at 14:02
-
@G00fY Thank you for the clarification. I have the
kotlin-android
plugin which is added to a new project by default. Mar 10, 2021 at 14:22
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
-
1Thanks 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?– vepzfeNov 20, 2020 at 7:17
-
1found the answer. Its not available in the current kotlin stable version yet - medium.com/@drjacky/where-should-we-add-plugins-b85517b8a62f– vepzfeNov 20, 2020 at 9:16
-
@G00fY Both 'kotlin-parcelize' and 'org.jetbrains.kotlin.plugin.parcelize' are working for me.– vepzfeNov 23, 2020 at 19:05
-
what about this line : classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" Dec 28, 2020 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. May 21, 2021 at 12:06
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
- 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
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
Just update your import
from: import kotlinx.android.parcel.Parcelize
to the: import kotlinx.parcelize.Parcelize