Dagger Hilt: Difference between revisions
Created page with "=Introduction= This page is to summarize new Dagger Hilt =Configuration= Dagger hilt relies on kotlin-kapt and dagger.hilt.android.plugin <syntaxhighlight lang="groovy"> plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'dagger.hilt.android.plugin' } dependencies { implementation "com.google.dagger:hilt-android:2.40.5" kapt "com.google.dagger:hilt-compiler:2.40.5" implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alph..." |
No edit summary |
||
Line 19: | Line 19: | ||
} | } | ||
</syntaxhighlight> | |||
=Modules= | |||
These still exist in Dagger Hilt. They can still be group by scope | |||
=Example Module= | |||
This is an example of an App Module. The InstallIn type decide how long the dependency will live. Examples are | |||
*ActivityComponent | |||
*ViewModelComponment | |||
*SingletonComponent | |||
*ActivityRetainedComponent Not destroyed when screen rotated | |||
*ServiceComponent | |||
Note the @Provides is no different to Dagger 2, the @Singleton, not to be confused with the above, decides how many instances to create of this module. In this case we want one instance across the app. If we did not specify @Singleton it would create one on each @Inject request. Note this example also shows an example where the MyRepository depends on MyApi. | |||
<syntaxhighlight lang="kotin"> | |||
@Module | |||
@InstallIn(SingletonComponent::class) | |||
object AppModule { | |||
@Provides | |||
@Singleton | |||
fun provideMyApi(): MyApi { | |||
return Retrofit.Builder() | |||
.baseUrl("https://www.bibble.co.nz") | |||
.build() | |||
.create(MyApi::class.java) | |||
} | |||
@Provides | |||
@Singleton | |||
fun provideMyRepository(api: MyApi, app: Application): MyRepository { | |||
return MyRepositoryImpl(api, app) | |||
} | |||
} | |||
</syntaxhighlight> | |||
=Example ViewModel= | |||
Now we are free to inject this into a ViewModel | |||
<syntaxhighlight lang="kotin"> | |||
@HiltViewModel | |||
class MyViewModel | |||
@Inject constructor( | |||
private val repository: MyRepository | |||
private val appContext: Application | |||
): ViewModel() { | |||
} | |||
</syntaxhighlight> | |||
=Example Activity= | |||
<syntaxhighlight lang="kotin"> | |||
@AndroidEntryPoint | |||
class MainActivity: ComponentActivity() { | |||
override fun onCreate(savedInstanceState: Bundle?) { | |||
super.onCreate(savedInstanceState) | |||
setContent { | |||
MyTheme { | |||
val viewModel = hiltViewModel<MyViewModel>() | |||
} | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
=Example Application= | |||
This is a bit easier than the old days.You will need to provide the Class name in the Manifest as you did with Dagger 2 e.g. name=".MyApp" | |||
<syntaxhighlight lang="kotin"> | |||
@HiltAndroidApp | |||
class Myapp: Application() | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 23:24, 12 March 2025
Introduction
This page is to summarize new Dagger Hilt
Configuration
Dagger hilt relies on kotlin-kapt and dagger.hilt.android.plugin
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'dagger.hilt.android.plugin'
}
dependencies {
implementation "com.google.dagger:hilt-android:2.40.5"
kapt "com.google.dagger:hilt-compiler:2.40.5"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation ("androidx.hilt:hilt-navigation-compose:1.0.0")
}
Modules
These still exist in Dagger Hilt. They can still be group by scope
Example Module
This is an example of an App Module. The InstallIn type decide how long the dependency will live. Examples are
- ActivityComponent
- ViewModelComponment
- SingletonComponent
- ActivityRetainedComponent Not destroyed when screen rotated
- ServiceComponent
Note the @Provides is no different to Dagger 2, the @Singleton, not to be confused with the above, decides how many instances to create of this module. In this case we want one instance across the app. If we did not specify @Singleton it would create one on each @Inject request. Note this example also shows an example where the MyRepository depends on MyApi.
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
@Singleton
fun provideMyApi(): MyApi {
return Retrofit.Builder()
.baseUrl("https://www.bibble.co.nz")
.build()
.create(MyApi::class.java)
}
@Provides
@Singleton
fun provideMyRepository(api: MyApi, app: Application): MyRepository {
return MyRepositoryImpl(api, app)
}
}
Example ViewModel
Now we are free to inject this into a ViewModel
@HiltViewModel
class MyViewModel
@Inject constructor(
private val repository: MyRepository
private val appContext: Application
): ViewModel() {
}
Example Activity
@AndroidEntryPoint
class MainActivity: ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyTheme {
val viewModel = hiltViewModel<MyViewModel>()
}
}
}
}
Example Application
This is a bit easier than the old days.You will need to provide the Class name in the Manifest as you did with Dagger 2 e.g. name=".MyApp"
@HiltAndroidApp
class Myapp: Application()