Monday, March 31, 2014

Android Dependency injection: RoboGuice vs ButterKnife

Dependency injection

Dependency injection is a software design pattern that implements inversion of control and allows a program design to follow the dependency inversion principle. The term was coined by Martin Fowler

Why Dependency injection?

  • Reduction of boilerplate code
  •  It promotes reusability, testability and maintainability.

There is any DI for Android?

Yes, there are many libraries, which support injection in android.
Listed below are used the most in this space.

RoboGuice

RoboGuice is a framework that brings the simplicity and ease of Dependency Injection to Android, using Google's own Guice library.   If you've ever used Spring (the #1 enterprise framework on Java, now more popular than J2EE itself) or Guice, you already know how convenient this style of programming can be.

Android Annotations

AndroidAnnotations is an Open Source framework that speeds up Android development. It takes care of the plumbing, and lets you concentrate on what's really important. By simplifying your code, it facilitates its maintenance.
  • Dependency injection: inject views, extras, system services, resources, ...
  • Simplified threading model: annotate your methods so that they execute on the UI thread or on a background thread.
  • Event binding: annotate methods to handle events on views, no more ugly anonymous listener classes!
  • REST client: create a client interface, AndroidAnnotations generates the implementation.
  • No magic: As AndroidAnnotations generate subclasses at compile time, you can check the code to see how it works.
  • AndroidAnnotations provide those good things and even more for less than 50kb, without any runtime perf impact!

Dragger

Fast dependency injector for Android and Java.
Dagger is a replacement for these FactoryFactory classes. It allows you to focus on the interesting classes. Declare dependencies, specify how to satisfy them, and ship your app. By building on standard javax.inject annotations (JSR-330), each class is easy to test. You don't need a bunch of boilerplate just to swap the RpcCreditCardService out for a FakeCreditCardService.
Dependency injection isn't just for testing. It also makes it easy to create reusable, interchangeable modules.

ButterKnife

View "injection" library for Android which uses annotation processing to generate code that does direct field assignment of your views.

Transfuse

Transfuse is a Java Dependency Injection (DI) and integration library geared specifically for the Google Android API.
There are several key features that make Transfuse a great framework to use with Android:
Dependency Injection - Transfuse implements the JSR-330 standard annotations
POJO Components - Transfuse gives users the ability to develop Android components in Plain Old Java Objects (POJO), enabling a testable, decoupled and flexible style.
Compile Time Code Generation - Transfuse is remarkably small, lightweight and fast due to the technique of generating supporting code at compile time.
Manifest Management - Transfuse manages the Android Manifest, eliminating the duplicated effort of declaring and registering components.
All of these features help eliminate boilerplate plumbing code and make Android applications much easier to write.

Which is the best Dependency Injection Framework?

It’s purely depending on the usage of your project.

For Example:
In android, if I want to avoid the boilerplate code such as injection of views, view holder in adapter and click listeners then the preferred library can be ButterKnife compare to others because remaining all done more than the required. There are tons of features which are not used added in all these libraries.

AndroidAnnotations  and ButterKnife does the similar work.
RoboGuice, Dagger and Transfuse are in the same category.

We will take one from each, say RoboGuice vs ButterKnife.

Let see, which is better for the example taken.

RoboGuice
ButterKnife
Minimum Jars required
3 (roboguice-2.0.jar, javax.inject-1.jar and guice-3.0-no_aop.jar)
1 (butterknife-4.0.1.jar)
Should replace Activity?
Yes, Replace Activity with RoboActivity
No Need
Can be used?
Yes, By adding one more jar (roboguice-sherlock-1.5) to the application and we should replace SherlockActivity to RoboSherlockActivity
Yes, No need of extra jars
Injection for Click Listeners
No
Yes
Performance
There is performance impact as it's done on runtime using reflection
Compile time
Pojo Injection
Yes
No
injection in Fragments
Yes
Yes
Injection in Adapters
No
Yes
Code License
Origin
Sample Source Code

Note: Managing Your App's Memory from developer.android

Avoid dependency injection frameworks
Using a dependency injection framework such as Guice or RoboGuice may be attractive because they can simplify the code you write and provide an adaptive environment that's useful for testing and other configuration changes. 

Be careful about using external libraries
External library code is often not written for mobile environments and can be inefficient when used for work on a mobile client.


Source Code
You can download the source code by clicking the links provided in the comparison table. This project is built using eclipse IDE. Unzip and import the project into Eclipse, it’s a good idea to use the Project by clean and rebuild from the project menu.

Thanks for reading :) 
Whether this post is helpful?


Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? Share it in the comments.

1 comment :