Open In App

Dagger 2 @Inject and @Component Annotations in Android

Last Updated : 18 Feb, 2022
Like Article

Android is an open-source operating system, based on the Linux kernel and used in mobile devices like smartphones, tablets, etc. Further, it was developed for smartwatches and Android TV. Each of them has a specialized interface. Android has been one of the best-selling OS for smartphones. Android OS was developed by Android Inc. which Google bought in 2005. So, in this article, we will learn how to use @Inject and @Component in our android app using Kotlin. Will we learn how Dagger creates the required object? How does it automate the manual injection process?


  • Construction injection
  • Manual injection
  • Basics of Dependency injection
  • Kotlin syntax

In the previous article, we saw how manual injection works, So here we will take that same project for understanding this concept. Dagger 2 Android implementation is easier and it is based on Dependency Injection Architecture. Dagger makes objects for the dependent class. Dagger needs to know how to create instances for the classes. Here we use @Inject annotation to the constructor, which will instruct Dagger, How to create an instance with the @Inject annotated constructor. Since in our app, UserRegistrationService class is dependent on EmailService class and UserRepo class. We need to add @Inject annotation to their constructor as well.

EmailService class:


class EmailService @Inject constructor() {
    fun send(to: String,from: String,body:String){
       Log.d("result","Email sent")

UserRepo class:


class UserRepo @Inject constructor() {
    fun saveUser(email:String,pass:String){
        Log.d("result","user data saved")

UserRegistrationService class:


class UserRegistrationService @Inject constructor(
    private val userRepo: UserRepo,
    private val emailService: EmailService
) {
    fun userRegistration(email:String,pass:String){

Then we need to create an interface for Dagger component, a component is just a collection of dependencies in our project that it can use to find out where it should get those dependencies when they are needed. So we need to annotate this interface with @Component. Inside the @Component interface, we need to define functions that return instances of the classes we need.

UserRegistrationComponent interface:


interface UserRegistrationComponent {
    // as we need the object of 
    // UserRegistrationService class
    // in mainActivity
    fun getUserRegistrationService(): UserRegistrationService 

getUserRegistrationService() method returns a object of UserRegistrationService, Dagger creates its object (which is to be return). So in mainActivity file, we will call this method. When we build this project, Dagger will create a field as Dagger appending with the component interface’s name. Like in our case it will be DaggerUserRegistrationComponent (  Dagger + UserRegistrationComponent) we need to build it, Then we can access its method.


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val userRegistrationService = DaggerUserRegistrationComponent.builder().build().getUserRegistrationService()

There is no change in XML layout file.


<?xml version="1.0" encoding="utf-8"?>
        android:text="Hello World!"
        app:layout_constraintTop_toTopOf="parent" />

So, In this way, we can implement Dagger into our project.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads