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

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}