Open In App

Manual Dependency Injection in Android

Last Updated : 14 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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. In this article, we will learn about manual dependency injection. So first, before learning Dagger dependency injection we should know why we need a framework such as Dagger. In manual dependency injection what we do manually is done automatically by dagger. So let’s understand this by making an app in which we need to register users, save their credentials in the database and send them a message of successful registration. We will add a log statement for the database and send a message of successful registration.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to Create a new project in android studio in kotlin.

Step 2: We will create three new Kotlin classes

  • class UserRepo – To save user credentials.
  • class EmailService – To send emails to users for successful registration.
  • class UserRegistrationService– To register users, this class is dependent on UserRepo and EmailService classes.

UserRepo class

Kotlin




class UserRepo {
    fun saveUser(email:String,pass:String){
        Log.d(TAG,"user data saved")
    }
}


EmailService class

Kotlin




class EmailService {
    fun send(to: String,from: String,body:String){
       Log.d(TAG,"Email sent")
    }
}


UserRegistrationService class, since this class is dependent on UserRepo and EmailService class. We need to provide it objects of the classes on which it is dependent. So we create a constructor for passing objects of other classes, Instead of creating those objects inside UserRegistrationService class. This is called manual dependency injection.

Kotlin




class UserRegistrationService(
    private val userRepo: UserRepo,
    private val emailService: EmailService
) {
    fun userRegistration(email:String,pass:String){
        userRepo.saveUser(email,pass)
        emailService.send(email,"abc@gmail.com","")
    }
}


Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.

Kotlin




class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val userRepo = UserRepo()
        val emailService = EmailService()
  
        val userRegistrationService = UserRegistrationService(userRepo,emailService)
        userRegistrationService.userRegistration("xyz@gmail.com","11111")
    }
}


Thus we can see here if UserRegistrationService class is dependent on two classes, it is easy for us to make its object manually. If a class is dependent on a number of other classes then we can use the Dagger framework for dependency injection. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads