Migrating deprecated Kotlin Android Extensions

Tram Ho

Since Kotlin 1.4.20-M2 JetBrains no longer uses the Kotlin Android Extensions compiler plugin.

Actually this has been expected for a long time, in this commit , you can see in Replaced kotlinx synthetic with findViewById:

kotlinx.android.synthetic is no longer a recommended practice. Removing in favorite of explicit findViewById.

Reason?

There are some big problems with kotlinx synthetic:

  • Revealing a global namespace consisting of ids that are not related to the layout is actually inflated without checking (for example, when having the same view ID in different layouts, when using it, it is possible to import the wrong view of another layout. error NullPointerException).
  • Can only be used in Kotlin.
  • Not really null safety when the layout is only available in some configurations (layout landscape, portrait …).
  • Also, kotlinx synthetic may not work on many modules, there was an open issue from January 2018.
  • All of these issues cause the API to increase the number of crashes for Android apps.

Alternatives?

  • View Binding is recommended for interacting with View, but it does have a few more nice things when compared to Android Kotlin Extensions. Compared to Android Kotlin Extensions, it adds compile-time checkups for view lookups and type safety.
  • the old traditional findViewById works for both Kotlin and Java.

JetBrains doesn’t use Kotlin Android Extensions in favor of View Binding, so we’ll explore how to switch to View Binding in this article.

View Binding

View Binding works with Java and Kotlin.

View Binding is a feature that allows you to write code to interact with the view more easily.

When the View Binding Inter view feature is enabled in a module, it creates a binding class for each XML layout file contained in that module. An instance of the binding class contains direct references to all the View IDs in the respective layout.

View Binding is not Null-safe for the layouts specified in multiple configurations. View binding will detect if a view is only available in some configurations and create a @Nullable property.

Note: Don’t confuse View Binding with Data Binding

How to enable View binding?

You don’t need to include any additional libraries to enable View binding. It is integrated into the Gradle Android Plugin starting with versions provided in Android Studio 3.6. To enable this in the modules that will use it, add the following to your build.gradle file.

If you want the layout file to be omitted when creating binding classes , add the tools: viewBindingIgnore = "true" attribute tools: viewBindingIgnore = "true" to the root view of that layout:

How to use View Binding?

If view binding is enabled for a module, a binding class will be generated for each XML layout file that the module contains. Each binding class contains references to the root view and all views with IDs.

The name of the binding class is created by converting the name of the XML file to Pascal case and adding the word Binding at the end, for example, the file named result_profile.xml , the binding class will be named ResultProfileBinding

Use View Binding in Activities

You can then access the Views using the binding object

Use View Binding in Fragments

You can then access views using binding same way as Activity.

Note:

  • The inflate () method needs to pass an inflater layout. If the layout has been inflated, you can use the static bind () method of the binding class .
  • Fragments last longer than its view. Make sure you remove any references to the binding class instance in Fragment’s onDestroyView () method.

About Parcelize feature in Kotlin Android Extensions?

The Parcelize feature in Kotlin is part of the kotlin-android-extensions plugin compiler, so deleting the plugin will prevent all of your Parcelable classes from compiling if they depend on the Parcelize annotation .

JetBrains extracted Parcelize from Kotlin Android Extensions into a new plugin, kotlin-parcelize

First, you’ll need to add the kotlin-parcelize plugin to your modules.

Then, change the old import statement from

Fort

You can find this IDE error

Forget it, the app is still building normally, this is a bug that has been reported on youtrack and has been marked as fixed.

Summary

Here’s what you should do to move from the kotlin-android-extensions plugin to ViewBinding and the kotlin-parcelize plugin:

  • Remove kotlin-android-extensions plugin from your build.gradle file.
  • Delete all kotlin synthetic imports from your activities and fragments.
  • Enable viewBinding in build.gradle in your module.
  • Add the binding object in your activities and fragments and use it to set the content view and access the views from your xml file.
  • If you are using Parcelize annotation, add plugins kotlin-parcelize to file build.gradle in your module and change the import statement as specified above.

Reference

Share the news now

Source : Viblo