Hilt makes Dagger easy
Tuesday, October 27, 2020Dagger 2 was powerful. Also painful to setup.
You needed a Component, a Module, a Subcomponent, another Module, scopes everywhere. Miss one annotation and you get cryptic compile errors.
Hilt fixed this.
@HiltViewModel
class SearchViewModel @Inject constructor(
private val repo: SearchRepository
) : ViewModel()
Thats it. No factory, no module, nothing else. Hilt knows what to do.
What Hilt does for you
- Creates the component hierarchy automatically
- Provides Android-specific scopes (Activity, Fragment, ViewModel)
- Handles lifecycle stuff you dont want to think about
The migration
We migrated from Dagger to Hilt in about a week. Most of the work was deleting code. Components, factories, all gone.
New developers dont need to understand Dagger internals anymore. They just add @Inject and it works.
If you still use Dagger 2 directly, try Hilt. You will delete alot of code.