This project demonstrates the usage of Hilt for dependency injection in three different scenarios:
- Direct Injection using
@InjectConstructor (Folder:simple) - Binding Interfaces with Hilt Modules (Folder:
bind) - Providing Dependencies with Hilt Modules (Folder:
provides)
simple/: Demonstrates direct injection using the@Injectconstructor.bind/: Demonstrates how to use@Bindsin a Hilt module for interface injection.provides/: Demonstrates how to use@Providesin a Hilt module for complex dependency provision.
When your dependencies can be provided through the constructor, you don't need to define a module. Hilt can automatically handle the creation of objects.
Example:
class ApiService @Inject constructor() {
fun fetchData(): String = "Data from API"
}
class Repository @Inject constructor(
private val apiService: ApiService
) {
fun getData(): String = apiService.fetchData()
}