DataBinding vs ViewBinding (Simple Comparison)
Background
In Android Development, every single time wants to access our layout, we always using `findViewById` to define our layout inside java/kotlin class and use correctly name to accessing or got crash when running the apps, but since android jetpack launch new feature that called “DataBinding”, developers have another option to define layout inside the java/kotlin class. And at least save you from expensive initialize layout and remove boilerplate and think its time to say Goodbye for `findViewById`.
After the jetpack launch, Android Studio 3.6 release new feature and called “ViewBinding”, and developers think
And we have some question between using Viewbinding and Databinding
- What difference implementation between them
- Is that any impacting to size
Let’s do this boy
What difference implementation between them
First, we must enable this in our app build.gradle
- DataBinding
apply plugin `kotlin-kapt` // if youre using kotlinandroid {
dataBinding {
enabled = true
}
- ViewBinding
android {
viewBinding {
enabled = true
}
and check your Gradle version, I preferably recommended with version 3.5 or higher for Databinding and ViewBinding, Then catch up with your layout add this part when using DataBinding
Then, how when we implement ViewBinding?
Just remove <layout> tag on the above layout
Yep, we don’t use <layout> ViewBinding, because on DataBinding we must add that layout tag to generate Binding class and it’s different with ViewBinding which automatically generates all layout to Binding class.
Okay, let’s go to MainActivity, below this for implementation DataBinding
and it’s an implementation using ViewBinding
All Binding class will be generated with a format below
first_activity.xml --> ActivityFirstBinding (Using Activity)
second_fragment.xml --> FragmentSecondBinding (Using Fragment)
item_movie_layout.xml ---> ItemMovieLayoutBinding (using item list layout)
Different between them is how to initialize that,
//DataBinding
binding = DataBindingUtil.setContentView(this, R.layout.activity_main) as ActivityMainBinding//ViewBinding
binding = ActivityMainBinding.inflate(layoutInflater)
Is that any impacting to size
I start to build APK between them, doing some comparison ( note: deactivate minify) and if you wanna see what I build visit my simple project here
Result APK size
Databinding = 2670 kb
ViewBinding = 2620 kb
There are different size apk 50kb between them and start to use analyze APK to check every part of them and I found inside classdex on androidx directory between DataBinding and ViewBinding generate the same directory except this
Remember, this is a simple project and maybe if you implement this on your complicated app. it makes a huge impact for your size APK/AAB ( Android App Bundles).
Final thought
Databinding and ViewBinding have different to implement and had an impact of size APK between them. 50 kb it’s important for our application despite we implement minify and R8 to reduce for our app and consider between using Databinding and Viewbinding for your app. it’s also worth using one of them to prevent a crash on your app and says
Goodbye findViewByID