Let's think about it this way for a second
Dependency Injection (DI) is a pattern where a class doesn't create its own dependencies but instead has them handed to it from the outside — this improves testing/maintainability (since you can inject fake dependencies for tests). Hilt is Google's official Android DI library — just put the `@Inject` annotation on a constructor, and Hilt automatically creates/provides the dependencies (Retrofit, Room database) for you — no more writing `Retrofit.Builder()...build()` by hand in every single ViewModel.
Let's connect this to a real scenario
If you write `@HiltViewModel class TodoViewModel @Inject constructor(private val dao: TodoDao) : ViewModel() { ... }` — Hilt automatically creates/injects the `TodoDao` instance, so you never need to manually pass a dao parameter into `TodoViewModel()` (you get it straight from the `viewModel()` Composable function).
Let's walk through it together
@HiltViewModel
class TodoViewModel @Inject constructor(
private val dao: TodoDao,
private val weatherApi: WeatherApi
) : ViewModel() {
// dao and weatherApi are automatically provided by Hilt —
// no manual Retrofit.Builder()/Room.databaseBuilder() calls here
}
@Composable
fun TodoScreen(viewModel: TodoViewModel = hiltViewModel()) {
// ...
}Grabbing TodoViewModel from hiltViewModel() should have dao/weatherApi ready to go without any manual passing.5-minute try-it
Read through the Hilt setup docs (`@HiltAndroidApp`, `@Module`, `@Provides` annotations) and sketch out how you'd provide `TodoDao`/`WeatherApi` as a Hilt module.
A quick word of caution
Hilt's annotation processing can slow down your build time a bit — keep in mind that the complexity of Hilt setup can be overkill for a small (learning) project.