Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Dependency Injection: RoboGuice vs. ButterKnife

DZone's Guide to

Dependency Injection: RoboGuice vs. ButterKnife

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

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.

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

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.

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

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.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}