Dependency Injection (DI) is a design pattern that allows you to remove hard-coded dependencies and make your code more modular, testable, and maintainable. Hilt is a popular DI framework for Android that simplifies the process of integrating DI in your application, following best practices recommended by Google. By using Hilt, you can easily manage your dependencies and reduce boilerplate code.
@HiltAndroidApp
class MyApplication : Application()
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var myRepository: MyRepository
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
// Your Compose UI code here
}
}
}
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
@Singleton
fun provideMyRepository(): MyRepository {
return MyRepositoryImpl()
}
}