Dagger 2 @Inject and @Component Annotations in Android
Last Updated :
18 Feb, 2022
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?
Pre-requisites:
- 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:
Kotlin
class EmailService @Inject constructor() {
fun send(to: String,from: String,body:String){
Log.d( "result" , "Email sent" )
}
}
|
UserRepo class:
Kotlin
class UserRepo @Inject constructor() {
fun saveUser(email:String,pass:String){
Log.d( "result" , "user data saved" )
}
}
|
UserRegistrationService class:
Kotlin
class UserRegistrationService @Inject constructor(
private val userRepo: UserRepo,
private val emailService: EmailService
) {
fun userRegistration(email:String,pass:String){
userRepo.saveUser(email,pass)
emailService.send(email, "abc@gmail.com" , "" )
}
}
|
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:
Kotlin
@Component
interface UserRegistrationComponent {
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.
Kotlin
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val userRegistrationService = DaggerUserRegistrationComponent.builder().build().getUserRegistrationService()
userRegistrationService.userRegistration( "xyz@gmail.com" , "11111" )
}
}
|
There is no change in XML layout file.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< androidx.constraintlayout.widget.ConstraintLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity" >
< TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Hello World!"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintLeft_toLeftOf = "parent"
app:layout_constraintRight_toRightOf = "parent"
app:layout_constraintTop_toTopOf = "parent" />
</ androidx.constraintlayout.widget.ConstraintLayout >
|
So, In this way, we can implement Dagger into our project.
Share your thoughts in the comments
Please Login to comment...