Tuesday, September 17, 2013

on Leave a Comment

5 Reasons You Should Use Butterknife for Android



I came across Butterknife a few weeks ago when I started looking for dependency injection libraries for Android. First let me be clear, BUTTERKNIFE IS NOT A DEPENDENCY INJECTION LIBRARY. I happened to find Butterknife while experimenting with Dagger . Butterknife injects code at compile time. It is very similar to the work done by Android Annotations

Essentially Butterknife allows you to annotate the inflation of your views and OnClickListeners  so that you can get back to writing the code that really matters. I have found it to be very easy to use and extremely useful. Unfortunately, none of my Android friends had ever heard of nor used this little library so I am doing my part to spread the word. With that said, here are 5 reasons you should be using Butterknife for Android:




1. You are tired of writing code like this:


This gets old really quickly. Fortunately Butterknife makes this go away.


2. The process for setting button OnClickListeners gives you nightmares.


It always seemed silly that you had to inflate a button simply to set its OnClickListener. Some people like to set the OnClickListener right after the view inflation. Others, prefer to implement OnClickListener in the fragment or activity and then switch on the id to call the correct method. While the latter method does clean things up somewhat, it is hardly ideal and it is still more code to write. Check out the two samples below. Which one would you choose?


3. You find AndroidAnnotations to be overkill for your project.


AndroidAnnotations is a cool idea. It has tons of features and more are being added all the time. Depending on which camp you are in, adding features can be both a blessing and curse. If you want to use annotations to inject everything imaginable then definitely give AndroidAnnotations a look. I am not 100% sold on the annotating and injecting of everything just yet. For now views, viewholders, and OnClickListeners is enough for me.

4. You are unhappy with the performance of RoboGuice and/or you use Dagger for dependency injection.


RoboGuice is a cool idea in theory. It is very powerful. It does a lot of the same things Butterknife can do but it does them at runtime using reflection. This causes a performance impact. Whether or not that performance impact is enough to scare you away is really up to you. Dagger+Butterknife seems to handle both the dependency inject and view injection with less of a performance impact.

For more info on adding Dagger to Eclipse click here.

5. You worship at the altar of +Jake Wharton and Square and you are looking for your latest sexy  Android library fix.



In all seriousness, these guys are awesome. I have learned so much reading through their github repositories and blogs. If you are at all interested in learning about the craft of software development then definitely give these guys a look. Besides, where would we have been without ActionBarSherlock while Google took its time adding an Actionbar to the support library?

Powered by Blogger.