Over a million developers have joined DZone.

Dependency Injection: RoboGuice vs. ButterKnife

DZone's Guide to

Dependency Injection: RoboGuice vs. ButterKnife

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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 - Wikipedia

 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
  • Android Annotations
  • Dragger
  • ButterKnife
  • Transfuse

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.

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
There is performance impact as it's done on runtime using reflection
Compile time
Pojo Injection
injection in Fragments
Injection in Adapters
Code License

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.

Download Modern Java EE Design Patterns: Building Scalable Architecture for Sustainable Enterprise Development.  Brought to you in partnership with Red Hat


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}