Dependency injection with Hilt

14737
0

Hilt simplifies dependency injection in Android with compile-time code generation. I annotate Application class with @HiltAndroidApp and activities/fragments with @AndroidEntryPoint. Modules marked with @Module and @InstallIn define how to provide dependencies. @Provides methods in modules create instances, while @Binds connects interfaces to implementations. @Singleton ensures single instance app-wide. @ViewModelInject or @HiltViewModel injects ViewModels. Hilt handles scopes automatically—Activity, Fragment, View, ViewModel. Field injection uses @Inject on properties. Constructor injection is preferred for testability. Hilt integrates with Jetpack components and reduces boilerplate compared to Dagger.