question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Alternative solution to view binding other than ButterKnife

See original GitHub issue

Disclaimer: this is a copy-paste from #395 as it seems that I was late to the discussion and I feel that my points warranted a separate issue.

One problem with using Butterknife (though minor) is that the fields must be at least package-private, which means the scope is unnecessarily broad in some circumstances.

An alternative that I am currently using in one of my projects is to use the generated view binder found using the data binding. Using this doesn’t mean that you have to use any of the features of the data binding library (nor would I advocate for such a thing). The main benefit that I found using the generated view binder is that it is type safe, something that neither Butterknife nor the standard Android findViewById offers i.e. you don’t have to cast the findViewById to a subclass of View.

Jake Wharton (the writer of Butterknife) even says that the use of it can be advantageous (source). Here’s an article which tells you basically all that you need to know about this approach.

I thought I’d just add this to the discussion as it doesn’t get talked about much, but would reduce the boilerplate code even more than that of Butterknife alone, is type safe, and IIRC findViewById traverses the layout at runtime whereas using the view binding aspect of databinding, view processing is done at compilation time.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
misaochancommented, Mar 12, 2017

@whym Given that the generated view binder can be used in conjunction with Butterknife, should we merge #409 and just consider the addition later?

0reactions
ilgazercommented, Jul 29, 2019

It seems that the project has settled on Butterknife and the generated view binder hasn’t seen any use.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin Android View Binding: findViewById vs Butterknife vs ...
ViewBinding : Google's recommendation nowadays. It's faster than databinding and prevents logic errors inside XML (hard to debug).
Read more >
Use view binding to replace findViewById | by Sean McQuillan
New in Android Studio 3.6, view binding gives you the ability to replace findViewById with generated binding objects to simplify code, remove ...
Read more >
The Evolution of View Linking in Android - Better Programming
The Data Binding Library generates binding classes for the layouts, which we can use in Android components like Activity and Fragment . Data ......
Read more >
How to Migrate from Butterknife to View Binding in Android
However, Butterknife still had its own issues such as null safety and speed. Now introduce View Binding: a low code, null safe, and...
Read more >
View Binding - Android Studio Tutorial - YouTube
In this tutorial, I will explain how to use Android View Binding in activities and fragments, and how it's different from Data Binding, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found