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(R.id.textView);

Roboguice inject view code:

@InjectView(R.id.textView) 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(R.id.textView);

Butterknife code:

@BindView(R.id. textView) TextView textView;

Traditional code:

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

Butterknife code:

@OnClick(R.id.button)



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

Conclusion

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.

Comments

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  4. Thanks for sharing. Keep sharing

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

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

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

    ReplyDelete
  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

    ReplyDelete
  9. Excellent Sharing. You have done a great job. I gathered lots of new information... Java Training in Chennai

    ReplyDelete
  10. 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

    ReplyDelete
  11. Nice blog..! I really loved reading through this article. Thanks for sharing such
    an amazing post with us and keep blogging... RPA Training in Chennai | Blue Prism Training in Chennai

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

    Android training in Chandigarh

    ReplyDelete
  13. Its a good informative article.
    Find out all the info regarding android
    Androiddose

    ReplyDelete
  14. myTectra Placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance

    ReplyDelete
  15. Very useful blog, Thank you for sharing this information

    Android App Development Company: https://www.biztechcs.com/android-application-development/

    ReplyDelete
  16. 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

    ReplyDelete
  17. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us. Do check | Get trained by an expert who will enrich you with the latest updates.
    Web Designing Course in chennai
    web designing training in chennai
    Web Development courses in Chennai
    Java Training in Chennai
    Java course in Chennai
    Java Training Institute in Chennai

    ReplyDelete
  18. 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

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

    Article submission sites
    Education

    ReplyDelete
  20. Great post man thanks for sharing this useful information but I was i serach for Jailbreak download finally i found one original and working PS3 Jailbreak & PS4 Jailbreak Games PKG for free follow the link to read more.



    f you are looking for an PS4 Jailbreak then your search is over now as we are giving you a chance to jailbreak your PS4,Visit PS4 Jailbreak download

    ReplyDelete
  21. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    best rpa training in chennai
    rpa training in chennai
    rpa interview questions and answers
    automation anywhere interview questions and answers
    blueprism interview questions and answers
    uipath interview questions and answers
    rpa training in bangalore

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


    ReplyDelete
  23. FIFA 19 Hack are 100% entirely free for mobile, ps4 and PC in addition to safe and also protected. fifa mobile 19 hack

    ReplyDelete
  24. " 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

    ReplyDelete
  25. Thanks For Sharing The Information The Information Shared Is Very Valuable Please Keep Updating Us Time Just Went On Reading The article Python Online Course Hadoop Online Course Aws Online Course Data Science Online Course

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

    ReplyDelete
  27. It's not easy to promote your app on the Google play store. You can buy Android App Review online from our site buyappreviewsandroid.com which is essential for your Android app on the Internet. Our app reviews will help you promote your app on Google play store.

    ReplyDelete
  28. This comment has been removed by the author.

    ReplyDelete
  29. 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.

    ReplyDelete
  30. This comment has been removed by the author.

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

    Corporate training in Machine learning

    ReplyDelete
  32. I wanted to thank you for this great read. Your blog is one of the finest blogs . Thanks for posting this informative article. app development company in bhopal

    ReplyDelete
  33. I am so happy after reading your blog. It’s very useful blog for us.

    Corporate training in Machine learning

    ReplyDelete
  34. 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 www.wscentre.com

    ReplyDelete
  35. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    JavaScript Training in Chennai

    JavaScript Training in Chennai

    ReplyDelete
  36. 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.

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

    ReplyDelete

  38. 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

    ReplyDelete
  39. This article is really helpful for me. I am regular visitor to this blog. Share such kind of article more in future. Personally i like this article a lot and you can have a look at my services also: I was seriously search for a Salesforce training institutes in ameerpet which offer job assistance and Salesforce training institutes in Hyderabad who are providing certification material. It's worth to join Salesforce training institutes in India because of their real time projects material and 24x7 support from customer desk. You can easily find the best Salesforce training institutes in kukatpally kphb which are also a part of Pega training institutes in hyderabad. This is amazing to join Data science training institutes in ameerpet who are quire popular with Selenium training institutes in ameerpet and trending coureses like Java training institutes in ameerpet and data science related programming coures python training institutes in ameerpet If you want HCM course then this workday training institutes in ameerpet is best for you to get job on workday.

    ReplyDelete
  40. Thanks for sharing this post, This is useful tips. We At WS Centre, A Committed Web Design And Development Company In Dubai. http://www.wscentre.com
    Thanks for sharing this post, This is useful tips. We At WS Centre, A Committed Web Design And Development Company In Dubai.
    Web Agency Dubai
    Dubai Web Design
    Ecommerce Web Design Dubai
    Best Mobile Apps Developer Dubai
    Seo Services In Dubai
    Digital Marketing Agency Dubai UAE

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

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

    Web Design Gloucester
    SEO Gloucester

    ReplyDelete
  43. Inderpaul Sookraj is one of the most prominent cryptocurrency and blockchain influencers in India. With a career spanning over 12 years as a software developer, Inderpaul’s interest in crypto and bitcoin heightened in 2017, and he delved deeper to understand the intricacies of the processes.

    To strengthen his expertise, Inderpaul Sookraj completed CCE (Certified Cryptocurrency Expert), the advanced level certification in the year 2018.

    Today, with the vastly gained knowledge and expertise in over three years, Inderpaul Sookraj has made several influencer videos explaining different types of cryptocurrencies, helping others understand how this virtual currency works. With his vast expertise in crypto and blockchain, Inderpaul has created a remarkable audience for himself. People reach out to him to gain insights on crypto investments and more around the subject. Cryptocurrencies are gaining momentum, and more and more people are getting involved in the trading and mining of crypto.

    Inderpaul Sookraj also helps his followers understand the fundamentals of the technological infrastructure empowering Blockchains behind the scenes.

    In the words of the expert, Inderpaul Sookraj, ”Cryptocurrency is the only way our economies can sustain without having to rely on the physical money transactions. If you wish to walk ahead with times, start getting acquainted with crypto in a more defined manner.”

    ReplyDelete
  44. App reviews are extremely important. This is why it is important to get and Buy App Reviews for your application as it is soon launched. With the help of Buy App Reviews Android, you will only get the best. In order to make sure that your business is competitive, it is important to Buy App Reviews from a reputable company. We offer services such as a review alert service that lets you know when your company's name pops up in a review or on social media. Contact now!

    ReplyDelete
  45. 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Android Performance: Avoid using ENUM on Android

Smart way to update RecyclerView using DiffUtil

Android O: Impact On Running Apps And Developer Viewpoint