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

abhiank picture abhiank · Nov 20, 2020 · Viewed 10.1k times · Source

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

Answer

G00fY picture G00fY · Nov 21, 2020

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