Create new Live Template group in Android Studio
Now create a new template by pressing +
again or copy an existing template to the newly created group. I usually copy an existing template but for purpose of this article, I'll show how to create one from scratch. Next, you need to give an abbreviation
for your new template. This is what you will be typing in the editor to access the template. I will name it byVB
which is shortened form of by viewBinding
. Put appropriate description so that other people in your team can understand what this template is about if you choose to share it. In Template text
section put the following code
private val binding by viewBinding($CLASS$::$METHOD$)
This is what will automatically be typed when you select the template from suggestions in the editor. The last bit is to select context, context lets Android Studio know where to apply this template. Select Kotlin->Class
for purpose of this template. Here is how final this looks
Creating Live Template for viewBinding delegate
Let us see this in action
viewBinding delegate with Live Template
Isn’t that cool? That’s just a declaration of binding instance though. We still need to make changes in code such that all views are accessed via this binding instance. Let’s see how we can minimize that effort.
Use Kotlin scope functions where applicable
We declared binding instance quickly but what about its actual usage? We still need to access views like binding.textView
and so on everywhere. Let's say you have this bunch of code in your Activity/Fragment somewhere
Accessing views using synthetics
one way to migrate this code is this
Access views using View Binding instance
But thanks to Kotlin you can do this
Access views inside Kotlin scope function
with
is one of the scope functions that language provides us. You might also need to use binding?.apply {}
at times. Read more about scope functions here and here
If you are thinking that its too much work to type in that with
and then move existing code inside {}
block, stop thinking and read on.
Use Live Template to insert scope functions
If you have worked on Android Studio for some time you know that you can select a bunch of code and then press CMD+OPT+T (ALT+CTRL+T on windows) on mac and open a context menu that lets you surround that selected code with blocks like if
, if/else
, try/catch
etc. We are gonna create a similar template for view binding with block. This template needs to go in a special template group called surround
. Since we have already gone through the process of creating a new Live Template I will just show important bits here. Here is the live template code that we need to put
with(binding) { $SELECTION$ }
similarly, you can create a template for apply
case.
binding?.apply { $SELECTION$ }
Here is the final configuration of this template
Android Studio live template for surround with scope function
Here it is in action
Once you are done with removing all synthetics from your app don’t forget to remove following piece of code from build.gradle
file. In android
block
androidExtensions { experimental = true }
from top
apply plugin: 'kotlin-android-extensions'
If you are using @Parcelize
annotation in your app follow this.
That’s it, folks. This workflow helped me in boosting the migration speed by 2x to 3x. I hope you can gain some speedups in your workflow too. If you have a trick up your sleeve as well then do share.
Happy Coding!
Originally published at https://abhishekbansal.dev on January 29, 2021.