Dependency Injection (Android): What/Why DI? [PART 1]
![Dependency Injection (Android): What/Why DI? [PART 1]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1660489976731%2FoMRzPbzd2.jpg&w=3840&q=75)
Senior Android Engineer from Bangladesh. Love to contribute in Open-Source. Indie Music Producer.
Search for a command to run...
![Dependency Injection (Android): What/Why DI? [PART 1]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1660489976731%2FoMRzPbzd2.jpg&w=3840&q=75)
Senior Android Engineer from Bangladesh. Love to contribute in Open-Source. Indie Music Producer.
No comments yet. Be the first to comment.
In this series, I will write about Android App Development.
Easy API Mocking with Retrofit & OkHttp in Android.
A practical guide to choosing the right coroutine primitive for your Android/Kotlin projects

This guide explains what Channels are, when to use them, how to use them correctly, and when NOT to use them β in simple, professional language. 1. What problem do Channels solve? In Kotlin coroutines, you often have multiple coroutines running at t...

Learn how to build preview-safe ViewModels in Jetpack Compose using Hilt.

HandlerInterceptor vs Filter in Spring Boot: A Practical Guide with JNDI Injection Prevention

Simple In-Memory Caching: A Tiny Trick with Massive Impact

If get to know something new by reading my articles, don't forget to endorse me on LinkedIn
According To Documentation, Dependency injection is a technique widely used in programming and well-suited to Android development. By following the principles of DI, you lay the groundwork for a good app architecture.
Implementing dependency injection provides you with the following advantages:
Reusability of code
Ease of refactoring
Ease of testing
Hilt is an opinionated dependency injection library for Android that reduces the boilerplate of using manual DI in your project. Doing manual dependency injection requires constructing every class and its dependencies by hand and using containers to reuse and manage dependencies.
Hilt provides a standard way to do DI injection in your application by providing containers to every Android component in your project and managing the container's lifecycle automatically for you. This is done by leveraging the popular DI library: Dagger.

Let's think DI as a provider to assist you on get requested objects without explicitly creating new object at your own.
In the above diagram, we see that Client asking for a dependency to the Provider, and Provider searching for the Requested Object in the Repository.
Now, here is the magic. How do they do it? Is it really magic? Hmm?
No, it's logic.
Here is the process ::
You register the required Object to the Provider Repository, which would be provided to you as a Dependency.
When you request for the specific Object to the Provider, Provider simply perform a search for the object in the Repository, here is the fun fact - If you request for an Object that is not properly registerd to the Provider, Android Studio will throw compile time error.
So, simply think. You have a Provider, who is responsible to provide you the requested depdency in run-time and the requested object should be registered to the Provider Repository.
Fun, right?
According to Official Documentation
Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project. Doing manual dependency injection requires you to construct every class and its dependencies by hand, and to use containers to reuse and manage dependencies.
Hilt provides a standard way to use DI in your application by providing containers for every Android class in your project and managing their lifecycles automatically. Hilt is built on top of the popular DI library Dagger to benefit from the compile-time correctness, runtime performance, scalability, and Android Studio support that Dagger provides. For more information, see Hilt and Dagger.
The most amazing things about DI/Hilt, is it's all about Annotation Processing.
"Annotation Processing" is a hook into the compile process of the java compiler, to analyse the source code for user defined annotations and handle then (by producing compiler errors, compiler warning, emitting source code, byte code). Ref
That's it for today. In next part, we will dicuss about Hilt and it's implementation.