Moving from Android Synthetic View Binding to Modern View Binding
I’ve met this kind of task on my work, so this is a little note about how things done.
Introduction
In recent years, Android development has evolved significantly, and so have its best practices. One such improvement is the introduction of modern View Binding, replacing the now-deprecated synthetic view binding. In this article, we will explain how to transition from the old synthetic view binding to the modern View Binding in a clear, explanatory way, complete with code examples. Let’s dive right in!
What is View Binding?
View Binding is a feature that allows you to interact with your UI components in a type-safe and efficient manner. By generating a binding class for each XML layout file, View Binding ensures that you no longer need to use findViewById
or synthetic imports to access your views.
Step 1: Removing Kotlin Android Extensions
To start the migration process, you need to remove the Kotlin Android Extensions plugin from your project, as it is responsible for generating synthetic view imports.
In your build.gradle
(Module) file, remove the following line:
apply plugin: 'kotlin-android-extensions'
Step 2: Enabling Modern View Binding
Next, enable View Binding by adding the following block inside the android
block in your build.gradle
(Module) file:
android {
...
viewBinding {
enabled = true
}
}
After syncing your project, Android Studio will automatically generate binding classes for your XML layouts.
Step 3: Refactoring Your Code
Now that you’ve enabled modern View Binding, it’s time to refactor your code. Let’s compare the two approaches with an example.
Synthetic View Binding
In the old approach, you would use synthetic view binding to access a TextView like this:
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
myTextView.text = "Hello, View Binding!"
}
}
Modern View Binding
With modern View Binding, you need to create an instance of the generated binding class and use it to access your views. Here’s how the same example looks after the migration:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.myapplication.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.myTextView.text = "Hello, View Binding!"
}
}
In the modern approach, we first create a binding variable, then use the inflate
method to instantiate the binding class. We then set the content view to binding.root
and use the binding object to access our views.
Step 4: Handling Null Safety and Type Safety
With modern View Binding, you don’t have to worry about null or type safety issues. The generated binding classes ensure that the views are never null and always of the correct type. This helps you avoid potential runtime crashes due to null pointer exceptions or type casting errors.
Conclusion
Migrating from synthetic view binding to modern View Binding might take some time, but the benefits are worth the effort. With a more reliable, type-safe, and efficient way to access your views, you’ll experience fewer crashes and a smoother development process.