Kotlin Android Extension- The deprecated warrior

Nagendra Hari Karthick
4 min readNov 4, 2020
Pic source: Google

Yes you heard me right. The plugin ‘̶k̶o̶t̶l̶i̶n̶-̶a̶n̶d̶r̶o̶i̶d̶-̶e̶x̶t̶e̶n̶s̶i̶o̶n̶s̶’̶ which made us to love Kotlin in each and every code we write is soon going to fade. At least that is what the following release notes says. People expects this to be deprecated from Kotlin version 1.4.20-M2.

As we all know and agree this is one of the best thing that happened to Android Devs from Kotlin Android Extension is Kotlin Synthetic. Developers Suffered for a long time using the traditional findViewById, then we have this plugin to rescue us.

Why do they want to deprecate it?

· Any layout Ids, can be accessed from anywhere without any proper validations. This might cause NPE in runtime.

· They don’t expose nullability

You can check about the detailed discussion about the issue with Kotlin synthetic here.

So what are the alternatives we have?

You can always use findViewById. Apart from that we have View binding.

View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. An instance of a binding class contains direct references to all views that have an ID in the corresponding layout.

In most cases, view binding replaces findViewById.

Let check How we can integrate ViewBinding to our existing application.

ViewBinding was first introduced in Android Studio Version 3.6. To implement ViewBinding your application just add in module level build.gradle file.

android {
viewBinding {
enabled = true
}
}

For Android version above 4.0 use

android {
buildFeatures {
viewBinding = true
}
}

Use ViewBinding in Activity

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)

}

}

My activity layout file activity_main.xml is being converted to an BindingClass ActivityMainBinding with the name in Pascal class and Binding added the end. You can access your id’s and their properties from binding variable.

binding.textView.text = "Hello World"

Use ViewBinding in Fragment

class BlankFragment : Fragment(R.layout.fragment_blank) {
private var binding: FragmentBlankBinding? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentBlankBinding.bind(view)
}

override fun onDestroyView() {
binding = null
super.onDestroyView()
}
}

We can pass the content layout id in Fragment constructor. By this time you would be able to guess the generated binding class name. Layout file name in Pascal case and appending Binding at the end.

Use ViewBinding for RecyclerViewAdapers

See it is simple. Just pass the generated binding class to your View Holder and inflate it in onCreateViewHolder. You are good to go. The same can be applied for other layout components.

However you might want to exclude some layout from generating Binding Class for such layouts just add

<androidx.constraintlayout.widget.ConstraintLayout
tools:viewBindingIgnore="true">
</androidx.constraintlayout.widget.ConstraintLayout>

Let’s Look into the binding classes

You can check the generated binding class from switch to project view in Android studio. Goto app> build> generated. You can find all the generated binding classes here. If you notice you find all the ids are declared as variable with type assigned and it also has root view defined. The complier basically converts all your xml code to java code with each id as an variable defined with it’s type.

We can also notice that inside the bind method findViewById is called internally to access to Views. See this also validates null.

At the End of day everything is just the same with different name

View Binding vs Data Binding

Just understand that this not an replacement for Data binding. We can use both Data and View Binding within the same module.

View Binding doesn’t support two-way binding.

View Binding provides faster compilation and it is easy to use. Since you enable view binding in a module it gets applied to all the newly created files.

Thanks for reading

--

--