Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 663
vepzfe Avatar asked Nov 20 '20 07:11

vepzfe


People also ask

Are kotlin extensions deprecated?

Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported.

Why is kotlin Android extension Gradle plugin deprecated?

They are not supported with Java. There are still projects that are written in Java and may not be fully migrated to Kotlin yet, and hence Kotlin Synthetics may not be a consistent way of getting the ViewIds in your project. Because of these issues, Kotlin Synthetics is now being deprecated.

How do I use kotlin-Parcelize on Android?

Usage. Just add the @Parcelize annotation to a class implementing the Parcelable interface and the Parcelable implementation will be generated automatically. This is the same example as in the previous article, in just 2 lines of code. The class can be a data class but it's optional.

What about the parcelize feature from Kotlin Android extensions?

What about the Parcelize feature from Kotlin Android Extensions? Don’t forget that Parcelize feature in Kotlin is part of the kotlin-android-extensions compiler plugin, so removing the plugin will endup making all your Parcelable classes not compiling if they depend on the Parcelize annotation.

What happened to Kotlin Android extensions compiler plugin?

In Kotlin 1.4.20-M2 JetBrains deprecated Kotlin Android Extensions compiler plugin. Actually this was expected long time ago, in this commit you can see kotlinx.android.synthetic is no longer a recommended practice. Removing in favour of explicit findViewById But why?

What is the Kotlin-parcelize plugin?

The kotlin-parcelize plugin provides a Parcelable implementation generator. When you annotate a class with @Parcelize, a Parcelable implementation is automatically generated, as shown in the following example: @Parcelize requires all serialized properties to be declared in the primary constructor.

How to configure Android fragments with @parcelize?

Configuring Android Fragments with @Parcelize 1 Parcelable. Parcelable is an Android interface that allows you to serialize a custom type by manually writing/reading its data to/from a byte array. 2 Kotlin Android Extensions. ... 3 Combining @Parcelize and Fragments. ...


1 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 
like image 134
G00fY Avatar answered Sep 30 '22 20:09

G00fY