Android : Difference between DataBinding and ViewBinding

前端 未结 2 1256
迷失自我
迷失自我 2021-02-01 01:00

We are using DataBinding since Jetpack release. Android documentation indicates that ViewBinding was added in Android Studio 3.6 Canary 11+.

I read the d

相关标签:
2条回答
  • 2021-02-01 01:38

    ViewBinding VS Databinding

    Here is why I wanted to explain ViewBinding and DataBinding together.

    As you can see, ViewBinding is a sort of subset of DataBinding libraries which means ViewBinding and DataBiding can do the same jobs in terms of binding layouts. And it would also mean with DataBinding, you might not need ViewBinding cause it will do what ViewBinding is supposed to do and also provide a bit of an extra functionalities such as 2way binding, and using variables in XML files.

    Then, this can lead to a question

    "Then let's just use DataBinding as it sounds much more fancy!"

    Hold on. As much as it sounds fancy, it is a pretty heavy loaded library which might cause a longer compile time. So if you are not going to use DataBinding only functionalities then might be better to consider ViewBinding as it does have some advantages in terms of build time and apk size.

    For more detail read this article

    0 讨论(0)
  • 2021-02-01 01:53

    According to the official docs:

    ViewBinding

    Only binding views to code.

    DataBinding

    Binding data (from code) to views + ViewBinding (Binding views to code)

    There are three important differences

    1. With view binding, the layouts do not need a layout tag

    2. You can't use viewbinding to bind layouts with data in xml (No binding expressions, no BindingAdapters nor two-way binding with viewbinding)

    3. The main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with databinding due to annotation processors affecting databinding's build time.

    In short, there is nothing viewbinding can do that databinding cannot do (though at cost of longer build times) and there are a lot databinding can do that viewbinding can"t

    0 讨论(0)
提交回复
热议问题