Kotlin Extensions!! Don’t let them extend it’s stay in your code

Narendra Nath Chatterjee
3 min readMay 19, 2023

--

A complete guide to removing kotlin synthetic in your code.

Introduction:

The present kotlin version is 1.8.21.
Kotlin synthetics are unsupported since 1.8.0.
So if you are still using kotlin synthetics your kotlin version is locked at 1.7.30 which limits your ability to use the latest APIs in your codebase including the new k2 compiler and jetpack advancements.

What is Kotlin Synthetics and why was it deprecated?

Kotlin Synthetics was a feature in Kotlin Android Extensions that provided direct access to UI elements in Android XML layouts without the need for manual view binding. It allowed developers to refer to views by their IDs directly in Kotlin code.

However, Kotlin Synthetics was deprecated because it had several limitations and drawbacks.
1. It relied on runtime reflection, which could negatively impact performance and increase the APK size.
2. It also lacked type safety, making it prone to runtime errors and null pointer exceptions
3. Additionally, Kotlin Synthetics didn’t work well with custom views or layouts.

What to do and how to do it?

You have to migrate to viewBinding(preferably) since that is what is recommended by Google. Or use databinding and replace all the instances of kotlin synthetics which looks like

import kotlinx.android.synthetic.main.activity_main.*

Here are the steps to follow.

Disable wildcard Import in your Android Studio.

Wildcard imports refer to importing a package instead of declaring specific class names being used from a package

Go to Settings > Editor > Code Style > Kotlin and select “use single name import”

Delete this line

import kotlinx.android.synthetic.main.activity_main.

Then go to your activity and Optimize imports with Ctrl + Alt + L. (Make sure you have Auto Imports on to make this process faster)

The wildcard import now gets changed to

import kotlinx.android.synthetic.main.activity_main.et_title
import kotlinx.android.synthetic.main.activity_main.btn_search_recipies
import kotlinx.android.synthetic.main.activity_main.rv_grid_layout

This ensures you have complete visibility on the views that are using the synthetic binding.

1.1 As an alternative you can delete the synthetic binding and disable auto import and navigate with Ctrl + F2 for the occurrence of the errors.

2 . Enable viewbinding or databinding if you havent already

https://developer.android.com/topic/libraries/view-binding https://developer.android.com/topic/libraries/data-binding

3. Use Regex Search and Replace to your advantage

As you can see in the code the names are usually names as name_name_name , name_name, name_name_name_name etc ..
You want to change this to binding.nameName , binding.nameNameName and so on .

The way you can search for this is using a regex as follows
(\w+)_(\w+)_(\w+)_(\w+) and so on.
Few Gotcha’s to take care of.
1. Make sure to turn on Match Case, Match Word and search by regex .

Now to make it change to binding.( your expression)
use this as the replacement text

binding.$1\u$2\u$3\u$4

The EndGame

The ultimate goal is to get rid of the ‘kotlin-android-extensions’ plugin from your app level build.gradle. However another feature given by this plugin is Parcelize, which helps you create parcelable classes and you might be using it in your code instead of Serializable .

The good news is Parcelize is now available in the standalone kotlin-parcelize plugin with unchanged functionality.

plugins {
id 'kotlin-parcelize'
}

That is it. So get rid of the extension functions and makes your codebase ready for all the new changes kotlin and android has to offer. I’m trying to get into the habit of writing blogs so a clap would be “amazing”.:D

--

--