Junior — Middle
Can you name popular dependency injection libraries besides Dagger?
sobes.tech AI
Answer from AI
Besides Dagger, popular libraries for dependency injection in Android include:
- Hilt — an official library from Google built on top of Dagger, simplifies DI setup in Android projects.
- Koin — a lightweight DI framework in Kotlin, does not use annotations or code generation, easy to use.
- Kodein — another Kotlin library for DI, convenient and flexible.
- Toothpick — a fast and simple DI framework with scoping support.
Example of using Koin:
// Defining a module
val appModule = module {
single { Repository() }
factory { ViewModel(get()) }
}
// Starting Koin in Application
startKoin {
modules(appModule)
}
// Injecting into ViewModel
class MyViewModel(val repo: Repository) : ViewModel() {
// ...
}
The choice of library depends on project requirements, team preferences, and architecture complexity.