Fundamentals of Dependency Injection and popular libraries in Android

Dependency injection is quite interesting topic and to understand it perfectly it is very important to learn the basics of it and learn it in depth. So I am planning to explain it in two parts.

This post will explain what dependency injection is, its benefit and different popular DI options which we can use in our Android projects. It seems little theoretical to you to grasp the basics. So let’s gets started…

“With good basics you’ll have endless options”

What is DI?

Any business application is made up of two or more classes and these classes collaborate with each other to perform some operation. Traditionally, we create the instance of dependent class object and do the required operations. When applying Dependency Injection, the objects are given their dependencies at creation time by some external entity that coordinates each object in the system. It means dependencies are injected into objects.

In other words, Dependency injection is a style of object creation in which an objects are created by an external entity, or technique whereby one object supplies the dependencies of another object.

It is built upon the concept of Inversion of Control which says “Rather than low level code calling up to high level code, high level code can receive lower level code that it can call down to. This inverts the typical control pattern seen in procedural programming.”

How DI works?

Most dependency injectors rely on reflection to create and inject dependencies. Reflection is awesome but is very slow and time consuming. Also it perform dependency resolution at runtime which leads unexpected errors and crashes the application.

On the other hand some injectors uses a Pre-compiler that creates all the classes (object graph) it needs to work using Annotation Processor. An Annotation Processor is a way to read the compiled files during build time to generate source code files to be used in the project. So it perform the dependency resolution before the application runs and avoid unexpected errors.

Dependency Injection Modes

There are three common means for a client to accept a dependency injection:

·       Constructor based injection
·       Setter method
·      Interface

Benefits of DI

Dependency Injection reduces to write boiler plate code and make the development process smooth. Implementing proper dependency injection in our apps allows us to have:

·       Loose coupling
·       Easily testable code
·       Code reusability

Popular Libraries comparison

The available popular dependency injection libraries for Android are:

·       RoboGuice
·       ButterKnife
·       Dagger
·       Android Annotation

Let’s compare above dependency injection libraries

Roboguice injects code at runtime using reflection. Using RoboGuice, we can inject Views, Drawable, Resources, System Service, or any other objects. Because of using reflection it is slow.

Traditional code:

TextView textView = (TextView) findViewById(;

Roboguice inject view code:

@InjectView( TextView textView;

Butterknife uses annotation processing to generate modified Java classes based on annotations. It allows to annotate the views and OnClickListeners. Butterknife also includes findById methods which simplify code that still has to find views on a View, Activity, or Dialog.

Traditional code:

TextView textView = (TextView) findViewById(;

Butterknife code:

@BindView( textView) TextView textView;

Traditional code:

button.setOnClickListener(new OnClickListener(){
public void onClick(View view){

Butterknife code:


Dagger is designed for low-end devices. It is based on the Java Specification Request (JSR) 330. It uses code generation and is based on annotations. The generated code is very relatively easy to read and debug.

Dagger 2 uses the following annotations:

  • @Module and @Provides: define classes and methods which provide dependencies
  • @Inject: request dependencies. Can be used on a constructor, a field, or a method
  • @Component: enable selected modules and used for performing dependency injection


Using dependency injection framework may be attractive because it simplify the code we write and also provide an adaptive environment that's useful for testing and other configuration changes.

RoboGuice is a cool idea in theory and very powerful. It does a lot of the same things Butterknife can do but it does them at runtime using reflection. This causes a performance impact.

Dagger and Butterknife seems to handle both the dependency inject and view injection with less of a performance impact.

On next post we will get some practical and coding work.

Stay tuned for upcoming articles. For any quires or suggestions, feel free to hit me on Twitter Google+

To find more interesting topics on Software development follow me at Medium.


  1. I agree with this, no wifi games free are being loved by many phone gamers.

  2. This is awesome. Hai Author Good Information that i found here,do not stop sharing and Please keep updating us..... Thanks.
    Hire android developers
    android development company

  3. Your device has become leggy and freezes all the time and you don't have enough space to take more pictures or install apps or your battery has started draining quicker than ever and your device overheats and needs to cool down then this app works for you in a perfect and great manner.
    Android Cleaner and Booster
    Phone Clean
    Clean Master

  4. Thanks for sharing. Keep sharing

  5. Thanks For Sharing . It IS very helpful For Everyone ....
    If You Are Looking Best Android training in Chandigarh click here

  6. very interesting information, thanks for sharing with us this topic.
    Android training in Chandigarh

  7. Android and IOS Application: The professionals working in this company make sure to design user-friendly app and with creative designs.

  8. This information you provided in the blog that is really unique I love it!! Thanks for sharing such a great blog Keep posting..
    Android training in Noida

  9. Nice post, I really like the content posted by you. Thank you for sharing these details here with us.
    Web Design Company | Web development Lucknow

  10. very nice post and blog are very informative thanks for sharing this post. it's really very helpful.

    Android training in Chandigarh

  11. Its a good informative article.
    Find out all the info regarding android

  12. Very useful blog, Thank you for sharing this information

    Android App Development Company:

  13. A great deal of informative and advantageous information imparted by you. This might benefit many seekers. One can learn and speak English in an effective way, just by downloading English Learning App on your own smartphone, which you can use whenever and wherever you want to practice your communication skills with experts.
    English with experts | English Speaking App

  14. I would like to thank you much about this great post. Its very useful and helpful for anyone looking for tips to help him learn and master in Angularjs. I like your writing style and I hope you will keep doing this good working.
    Angularjs Coaching in Bangalore
    Angularjs Institute in Bangalore
    Best Angularjs Training in Bangalore
    Best Android Training in Bangalore
    Android Developer Course in Bangalore
    Android App Development Training in Bangalore

  15. Awesome post. Really you are shared very informative concept... Thank you for sharing. Keep on updating…

    Article submission sites

  16. Thanks for information , This is very useful for me.
    Keep sharing Lean Six Sigma Green Belt Training Bangalore

  17. " Write more; that’s all I have to say. It seems as though you relied on the video to make your point. You know what you’re talking about, why waste your intelligence on just posting videos to your blog when you could be giving us something enlightening to read?
    Authorized iphone service center in Chennai | iphone service center in chennai | Mobile service center in chennai | Authorized iphone service center in Chennai | iphone service center in chennai

  18. Good Post. I like your blog. Thanks for Sharing
    Java course in Delhi

  19. This comment has been removed by the author.

  20. Really Liked the information you have provided. I have an article relaed to it. I was searching about it on the internet and I found an amazing site on a Profile. The provided Article was about a site that provides working modded android apps. The name of the site was “Fineapkapps”. The Article was very halpful, You should read that. Click Here to reach that amazing article: New Modded Apps.

  21. This comment has been removed by the author.

  22. Thanks for sharing this valuable information and we collected some information from this post.

    Corporate training in Machine learning

  23. The Web Design Company in Dubai helps in checking the local intent query and uses the approach for ranking the website in the popular search engines like Google, Yahoo, etc. by finding the keywords. Simply by typing the keywords in the tool, one can come to know about the search volume.

    Visit Visit Website

  24. I Remmber that it is the very nice effective post about blog. I am searching this type of post then I got finally in this post. Anyone can check Best Clipping path Service and Clipping path servicedetails.Thanks for You Sharing.

  25. It is a very nice blog. Really it is a very international destination post. Thanks for sharing this post.
    Website development company in Bangladesh


  26. Thanks for sharing such a good content with us. keep share these kind of content.i would like to read more.
    Android Course in Delhi

  27. Students अपने विश्वविद्यालयों के Results के बारे में नवीनतम अपडेट की जाँच करें। यह साइट आपकी सभी क्वेरी को समझने में मदद करेगी है।
    BA Exam result Part 1st, 2nd, Final Year
    BCom Exam result 1st, 2nd, 3rd semester

  28. I love the way you write and share your niche! Very interesting and different! Keep it coming!

    Web Design Gloucester
    SEO Gloucester

  29. Hydrogen Fun Facts

    Hydrogen -
    The Most Abundant Element of the Universe. The hydrogen atom is the first and simplest atom, containing only a single proton and electron. Atomic mass: 1 u.This is also one of the fun facts about hydrogen.

  30. The Tata AIG Insurance ensures peace of mind with comprehensive coverage. Trust in our commitment to protect you and your assets, providing security for life's uncertainties.

    #Tata AIG Insurance #Insurance

  31. Hey there! Very good article.

    🚀 Unlock financial freedom with Money Back Term Insurance! 💰 It's not just insurance; it's your key to security and flexibility. 🌟 Explore the path to a brighter financial future! #MoneyBackInsurance

  32. Nice post, I really like the content posted by you. Thank you for sharing these details here with us.
    Manjummel Boys" captivates with its heartfelt portrayal of friendship amidst the vibrant culture of Kerala. The film delves into the complexities of youth, weaving a narrative rich in emotion and authenticity. With stellar performances and a compelling storyline, it leaves a lasting impression, resonating with audiences long after the credits roll.

  33. This comment has been removed by the author.

  34. 🚗💥 Who Pays in a Car Accident? 💥🚗
    Understand who pays for damages after an accident based on Car Accident insurance, state laws, and fault determination. Stay informed and drive safe! #CarAccident #AutoInsurance #InsuranceTips #SafeDriving
    🔹 Types of Insurance
    🔹 Fault Determination
    🔹 Special Cases
    🔹 What to Do After an Accident


Post a Comment

Popular posts from this blog

Android Performance: Avoid using ENUM on Android

Android O: Impact On Running Apps And Developer Viewpoint

Smart way to update RecyclerView using DiffUtil