How to use dependency injection with hilt in Kotlin 2.0.0+





 Step 1 : Adding the dependencies of dagger hilt 

in build.graddle(project level)

id("com.google.dagger.hilt.android") version "2.49" apply false


in build.graddle (module level)

[plugins]

id("kotlin-kapt")
id("com.google.dagger.hilt.android")


[dependencies] 

implementation(libs.hilt.android)
kapt(libs.hilt.android.compiler)

Additionally 

kapt {
correctErrorTypes = true
}



Now add the dependecies inside the libs.version.toml 

[versions]

hiltAndroid = "2.49"

[libraries]

hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroid" }
hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hiltAndroid" }


....sync the project 



to use hilt inside the viewmodel we need to add the hilt-navigation-compose 


implementation(libs.androidx.hilt.navigation.compose)
androidx-hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "hiltNavigationCompose" }
hiltNavigationCompose = "1.2.0"



Step 2 : Create a Base Class that implements Application() and annote as @HiltAndroidApp

@HiltAndroidApp
class Base : Application()

Also add this base class in our Manifest File (in Application Tag)

android:name=".Base"


Step 3: Annotate your main activity as @AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

Step 4: Creating our Modules for providers 

for this we create an object and anotate this with @Module and @InstallIn(SingletonComponent ::class)

then we creates the method where we annote them with @Provides and @Singleton for single instance throughout the whole application 


@Module
@InstallIn(SingletonComponent::class)
object HiltModule {

@Provides
@Singleton
fun provideDatabase( @ApplicationContext context : Context) : SharedPreferences{
val database = context.getSharedPreferences("private", Context.MODE_PRIVATE)
return database
}

} 

Additionally, inside hilt module we can get the application context as our dependency with the help of annotation @ApplicationContext 


Alright, this is ready to go. Now we can use this database inside our app as a dependency.


Step 5: to get this inside our class we can use two types by constructor injection or inline injection 


This is the way to inject inline 

@Inject
lateinit var database : SharedPreferences

Here it will automatically searches in our module for is there a method that provides us the SharedPrefences if so it will inject that in our databse variable. 


For two instance providing the same SharedPrefence then it is best to use the @Named annotation or by making Qualifiers 


For making Qualifiers 

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class PublicDatabase


@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class PrivateDatabase



Now for provide method mark this qualifiers 

@Provides
@Singleton
@PrivateDatabase
fun provideDatabase( @ApplicationContext context : Context) : SharedPreferences{
val database = context.getSharedPreferences("private", Context.MODE_PRIVATE)
return database
}


@Provides
@Singleton
@PublicDatabase
fun provideDatabase2( @ApplicationContext context : Context) : SharedPreferences{
val database = context.getSharedPreferences("public", Context.MODE_PRIVATE)
return database
}


Also wherever we are injecting this mark there also with qualifier to distinguish 


same goes for named 


mark provides with @named("public") and same mark where we are inecting it 


Now for the constructor injection 

lets take the viewmodel where we need the instance of the sharedpreference 

@HiltViewModel
class MainVM @Inject constructor(

@Named("public") val database : SharedPreferences
):ViewModel() {


to use in our compose we can make the hiltviewmodel as 

val mainVM : MainVM = hiltViewModel<MainVM>()

.... TO BE CONTINUED.....

Post a Comment

0 Comments