Dagger Hilt In Android
Posted By : Mohd Anas | 10-May-2021
Dagger Hilt
Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project. Dagger hilt makes the whole dependency process easier because we basically gives the instruction to dragger how to create an object and when what scope we want to have it in and we need to provide him in a correct place and a correct time and hilt is just a library of jetpack to on top pf dagger that makes using dagger 2 easier because dagger has always been a quiet complicated it requires a lot of code to set up but hilt generates a lot of code fast. it's perfect for android and it's quite a lot easier to use instead of a normal dagger.
Adding dependencies
First, add the hilt android gradle plugin to your project's root build.gradle file like this :
buildscript {
ext.hilt_version = '2.35'
dependencies {
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
}
}
Then, add the following dependencies in your app/build.gradle file:
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
dependencies {
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
}
Hilt uses the Java 8 features. So, we need to enable Java 8 in your project to add the following to the app/build.gradle file:
android
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
// for kotlin
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}
Hilt application class
First, we are going to create an Application Class to use the dagger hilt. this Application class tells the project that we are going to use the Dagger hilt.
This Application class that annotates with the @HiltAndroidApp which will just trigger code generation that this library needs.
@HiltAndroidApp
class ExampleApplication : Application() { ... }
And, then we have to tell our app that we want to use this application class to replicate this whole application basically otherwise it doesnt have any effect.
android:name=".ExampleApplication"
basically, this is the application-level class so every component can access the dependencies easily that this application class provides.
Dagger Modules
We are going to create a singleton instance of retrofit by using the dagger hilt.
Here is the example of code
// this module turns this object into a dagger module
@Module
// this annotation generates the dagger components to the application level
@InstallIn(SingletonComponent::class)
object AppModule {
// This turn it into dagger provides method
// which the dagger tells how to create an
// object in case of retrofit object
// and we annotate this to the @Singleton annotation because because it doesn't make sense to create multiple instances it just is a waste of memory.
@Provides
@Singleton
fun provideRetrofit(): Retrofit =
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
@Provides
@Singleton
fun provideApi(retrofit: Retrofit): ApiInterface =
retrofit.create(ApiInterface::class.java)
}
We don't need to call these @Provides methods anywhere in our code. such these provides methods only give the instruction for dagger because when we then later save we require API instance to inject, the dagger will look where can find instructions for it and this if what this modules for those are the just instructions for the dagger that receive how I create Api Instance for that I need retrofit this is how I create the retrofit instance and this is the whole chain. and we also annotate dagger in what scope we want to use this object because the object can live in the lifecycle of activity and fragments in our case, we need this object available in the lifetime of the whole application and we also want a singleton of this object so we annotate this object with @Singleton annotation because it doesn't make sense to create multiple instances it just is a waste of memory.
Inject dependencies into Android classes
Once an application-level component is available and Hilt is set up in your Application class, it can offer dependencies to other Android classes. These classes have the @AndroidEntryPoint annotation:
@AndroidEntryPoint
class ExampleActivity : AppCompatActivity() { ... }
Hilt currently supports the following Android classes:
- Application (by using @HiltAndroidApp)
- ViewModel (by using @HiltViewModel)
- Activity
- Fragment
- View
- Service
- BroadcastReceiver
If you annotate an Android class with @AndroidEntryPoint, then you also must annotate Android classes that depend on it. For example, if you annotate a fragment, then you must also annotate any activities where you use that fragment.
Reference
Android Developers : https://developer.android.com/training/dependency-injection/hilt-android
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Mohd Anas
Anas is working as an Android Developer in Oodles Technologies. He is hard working, Self Motivated and dedicated towards his work. He is also passionate about learning new technologies related to our fields.