This project demonstrates the usage of Hilt for dependency injection in three different scenarios:
- Direct Injection using
@Inject
Constructor (Folder:simple
) - Binding Interfaces with Hilt Modules (Folder:
bind
) - Providing Dependencies with Hilt Modules (Folder:
provides
)
simple/
: Demonstrates direct injection using the@Inject
constructor.bind/
: Demonstrates how to use@Binds
in a Hilt module for interface injection.provides/
: Demonstrates how to use@Provides
in 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()
}