[Jetpack] Simple comparison between ViewBinding and DataBinding





一、DataBinding



DataBinding started to be used in Android Studio 2.0 in 2016 , and its role is to realize the binding between the data model Model and the view View , which is a two-way binding;

  • The data model Model is the instance object of the data class;
  • View View refers to the Xml layout file;

DataBinding is to configure the data model instance object into the Xml layout file ;


The code logic that DataBinding can replace:

  • findViewById
  • setOnClickListener
  • setText
  • setVisibility
  • setEnable
  • setXxx

Obviously, DataBinding can get components in Activity / Fragment / Dialog, set click events, set text, set visibility, etc. in Xml layout files ;





Two, ViewBinding



ViewBinding is used in Android Studio 3.6 version in 2019. Its function is to generate binding classes for Xml layout files . With the help of this binding class, components in Xml can be quickly obtained;


The code logic that ViewBinding can replace:

  • findViewById
  • Define the member fields of the View component

The functions of ViewBinding and ButterKnife are basically the same , and the compilation performance is better than ButterKnife, because ButterKnife uses a lot of annotations, and its compilation speed is lower than that of ViewBinding;


Compared with DataBinding, ViewBinding is a lighter function library;

  • Integrated DataBinding is much slower than ViewBinding compilation;
  • The compiled Apk integrated with DataBinding is dozens of KB larger than ViewBinding;

If you just want to omit the findViewById code, use ViewBinding;

Guess you like

Origin blog.csdn.net/han1202012/article/details/130568901