6

Since I discovered the use of view binding by enabling

   buildFeatures {
         viewBinding true
     }

in my gradle file, I never used findviewById in my code again. I wonder now if there is a cons to doing things this way. If this is the best method, why does android studio not enable this option by default when creating a new project? If not, when should I avoid using view binding? thank you.

kfir88
  • 380
  • 2
  • 16
  • read it [here](https://developer.android.com/topic/libraries/view-binding#findviewbyid) – i30mb1 Dec 13 '20 at 11:05
  • Thank you but i think this link is talking about viewbinding and data binding. this not exactly what want here. – kfir88 Dec 13 '20 at 14:25

1 Answers1

6

Once you enable it for a project, view binding will generate a binding class for all of your layouts. That's the only "con" I see, it just generates more code, so it would increase the size of the project, compile time, etc. While it won't be a huge difference for projects with very little layouts, it could change significantly for larger projects.

Here's a very interesting read about ViewBinding performance : https://blog.stylingandroid.com/view-binding-performance/

Praveen P.
  • 976
  • 2
  • 11
  • 23
  • 1
    Thank you @Praveen. can you please tell me why do you think the generation of binding classes is an inconvenient? – kfir88 Dec 13 '20 at 11:31
  • Because it can increase the size of the project, therefore, the build time. It probably won't be noticeable in small projects, but it can make a difference in a larger project with loads of layouts. Although the benefits of ViewBinding may significantly outweigh the small build time hit. I was just mentioning it because you asked for a con, and this is the only con I could think of ;) – Praveen P. Dec 13 '20 at 11:40
  • This is NOT TRUE, you can easily exclude a layout from being generated by adding tools:viewBindingIgnore=“false” to the root – Darshan Dec 13 '20 at 13:21
  • well, yes, of course, but then you're not using ViewBinding. He was asking about the cons of using it. Also, having to add that to every layout that you don't want to generate a binding class is quite some work – Praveen P. Dec 13 '20 at 14:10
  • I said `you can easily exclude a layout` if you don't want to create ViewBinding class for that layout, not `all`. Its completely a developer's choice. In that specific case, your CON is not actually right. – Darshan Dec 15 '20 at 13:06
  • That link doesn't mention anything about performance – htafoya Mar 18 '21 at 18:00
  • you're right, sorry about the wrong link: this is the one I was meant to post https://blog.stylingandroid.com/view-binding-performance/ – Praveen P. Mar 21 '21 at 15:42