Dagger Demystified | Dagger Is No Longer Difficult

What is the Meaning of Dependencies?

class Car {
private val engineInstance : Engine
init {
engineInstance = Engine()
startCar()
}
private fun startCar(){
engineInstance.startEngine()
}
}
  • Here in a Car class we have instance variable of type Engine. In the init{} block Car constructs a new Engine object and assigns that to engine Instance variable and later it calls to startCar function.
  • We can see Car object uses that engine instance variable to call the start function of newly created engine object

So we can consider engine class as a dependency for the Car class

Why Dependency Injection?

init {
engineInstance = Engine()
startCar()
}

What Is Dependency Injection

Dagger makes our life easier by generating all the code for us considering the instructions given by us using a set of annotations

Constructor Injection

STEPS TO IMPLEMENT DAGGER 2:-

  1. We want Dagger to construct a SmartPhone and send it to Main Activity
  2. Now first of all Dagger has to construct all classes instance which SmartPhone class needs like Battery and Memory card instance. After that with these all Dagger can construct a SmartPhone Instance.
  3. First of All use @Inject annotation:- Whichever dependencies are required must be annotated with Inject annotation, It tells dagger that these dependencies / objects are to be injected from outside
  4. Now considering the instructions provided by us Dagger can generate code to construct dependencies represented by this dependency graph.
  5. To use dagger 2 generated code for dependency injection we also need an interface annotated with @Component.
@Component
interface SmartPhoneComponent {
fun getSmartphone() : SmartPhone
}
  1. Function name can be anything and Return type of function will be the dependency / object required. Dagger will only consider this return type
  2. If you see generated folder you will see that Dagger will create factory classes for all dependencies (i.e MemoryCardFactory, BatteryFactory etc)
  3. For your component interface Dagger will create a Class which implements that Interface component and it always includes the word Dagger in front of interface name

Here you can call either create() or builder() methods. These both functions creates a new object of this Dagger Component.

MODULES

  1. Now we will have to create a Module class for Memory card i.e MemoryCardModule and annotate it with @Module
  2. Create a method to provide Memory card instance from that module and annotate this method with @Provides (This annotation tells the dagger how to provide classes that your project doesn’t own or it tells Dagger how to create instances of the type that this function returns).

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Puneet Grover

Puneet Grover

Senior Software Engineer (Consultant) Android Apps @Deloitte USI | Google Certified Android Dev | Java | Kotlin | Blogger | Sometimes Youtuber