Dependency Injection In Android: Dagger vs Hilt

Mayur Waghmare
Mobile Innovation Network
3 min readJun 13, 2024

Dagger is one of the most popular and most used libraries for building dependency injection, especially in Android.

But it comes with some disadvantages:

  • High Learning Curve: Dagger’s advanced concepts and use of annotations can be challenging for developers new to dependency injection.
  • Lots of Boilerplate Code: Setting up Dagger requires writing extensive configuration code, leading to boilerplate code and decreased readability.
  • Difficult to Debug: Dagger’s compile-time code generation and complex graph can result in compiler errors and runtime exceptions, making debugging challenging.

So here is another powerful DI weapon for beginners and old dagger users:

Hilt

Hilt emerges as a newcomer to the scene, built on top of Dagger with the aim of simplifying dependency injections.

Pros of Hilt:

  • Low Learning Curve: Hilt simplifies dependency injection with simple annotations and components, reducing the learning curve.
  • Reduced Boilerplate Code: Hilt reduces the amount of boilerplate code using its automatic generation of Dagger components and modules.
  • Integration with Android Components:: Hilt offers predefined annotations and components tailored specifically for Android development, making it easier to integrate with common Android components like Activities, Fragments, and ViewModels.

Let’s look at the implementation for both Dagger and Hilt:

1. Component:

In Dagger-Android, we have to create a component class with a builder/factory and include every module.

But in Hilt, Just annotate the @HiltAndroidApp annotation, Hilt will do everything itself.

2. Module:

In Dagger, if you’re looking to provide dependencies at the application level you define your module and install it in the ApplicationComponent.

Whereas in Hilt, it uses @InstallIn to specify where modules should be installed.

3. ContributesAndroidInjector:

In Dagger, the ContributesAndroidInjector annotation was introduced for generating Subcomponent. But it also brings boilerplate code in our huge project.

In Hilt, we don’t need to write those annotations and ActivityModule. Instead, Hilt provides a @AndroidEntryPoint annotation.

4. ViewModel:

In Dagger we declare ViewModels using following code:

In Hilt, we don’t have to write the ViewModelFactory and ViewModelModule. Instead, Hilt provides @ViewModelInject annotation and @Assisted annotation for SavedStateHandle.

Conclusion

Dagger is better choice, If you need
- Fine-grained control.
- High flexibility.
- Complex projects.

While Hilt is recommended due to its
- Simplicity and Ease of Use .

Thank you for taking the time to read this article. If you found the information valuable, please consider giving it a clap or sharing it with others who might benefit from it.

Any Suggestions are welcome. If you need any help or have questions for Code Contact US. You can follow us on LinkedIn for more updates 🔔

--

--

Mayur Waghmare
Mobile Innovation Network

"Mobile App Developer specializing in Android, iOS, and Flutter. Passionate about crafting user-focused, efficient mobile solutions."