Hunting Kotlin Synthetics For Views

Hunting Kotlin Synthetics For Views

March 2, 2022·
Nishant Srivastava

Banner

Kotlin Synthetics for Views is deprecated! You can read about it here.

That means if you have been using them in your project then you need to replace them with a more recommended approach i.e ViewBindings. There is a migration guide too.

But before you can start the migration you need to track down the files where Kotlin Synthetics are being used. Lets find out.

Run the below command at the root of your project in a terminal window:

grep -rwl . -e 'kotlinx.android.synthetic'

here

  • -r = Recursive
  • -w = Match the whole word
  • -l = Output the file name of matching files
  • -e = Match pattern used during the search

On executing the above command, the output is like below:

❯ grep -rwl . -e 'kotlinx.android.synthetic'
./UsingKotlin/app/src/main/java/nisrulz/github/sample/usingkotlin/MainActivity.

Incase you are only interested in finding out the number of files where Kotlin Synthetics are being used, run the below command:

grep -rwl . -e 'kotlinx.android.synthetic' | wc -l

On executing the above command, the output is like below:

❯ grep -rwl . -e 'kotlinx.android.synthetic' | wc -l
1

That is all! Once you have found the places in your codebase where Kotlin Synthetics have been used, simply replace them with ViewBinding one by one. You could also completely switch to Jetpack Compose, if you want to switch to the next generation of UI development framework on Android.

Last updated on