Butter Knife vs Android Annotations
4/25/2015, 4:04:24 PM
There was a tech conference geared towards Android app developers, #DroidKaigi in Japan. I wasn’t able to attend unfortunately because it happened in Tokyo while I was in New York, but thanks to the folks who share most everything to the Internet, I could feel how it went. It sounds it went really well. Congratulations to the event organizers.
One thing that got me when I was reading those blogs and summaries about the conference was that Butter Knife sounded more popular and better loved by them than Android Annotations. I just sent a new Android app to our quality assurance process. The app was completely written from scratch by myself, which uses Android Annotations everywhere and I was a little concerned that I might have chosen the wrong library (again…?).
But it turned out that I wasn’t that much wrong. Butter Knife solves a problem that Android Annotations also solves, but it doesn’t have a critical part (for me) that Android Annotations helped me a lot while working on my app; Extra, FragmentArg and InstanceState.
Without these annotations, I have had to write _very correct_ code to handle saveInstanceState and onCreate and everything else. Now it might sound trivial - you just have to pass objects to an Activity you are starting and the code to handle them is well pattern-ed by now - but when it comes to handling orientation changes it gets incredibly hard to write “the correct” code. Fortunately, Android Annotations does the right thing for you with this tiny cost of you have to be careful that you are going to start Activity_ (with the trailing underscore), not the Activity you write (AndroidManifest.xml).
To be honest, we did have problems with Android Annotations in developing the previous versions of our app, especially with EFragment and AfterViews injections, but it turned out it was our fault - mostly because we wrote code that does “view initialization” in AfterViews methods, which turned out to be called every time the view is created. In Android, orientation changes destroy views and they are recreated. With some of those “gotchas” in mind, Android Annotations served really well for me this time.
To make it a little clear, I didn’t write code that use Butter Knife. I didn’t use EBean, EService, Rest or any other fancy attributes that Android Annotations offer either. EActivity, EFragment, ViewById, Click, OptionsMenu, OptionsItem, Extra, FragmentArg, and InstanceState are the ones I used and loved. I might try to use some more in the upcoming versions, but those only still make significant difference. I understand when someone say Android Annotations is simple overkill; it did concern me when I think of those unused features that Android Annotations offer, but hey, I managed to keep away from Guava and the app still compiles without multidex enabled.
I will write more about what I learned when I was writing the Android app soon.