Dependency Injection in Android – Dagger2 and Hilt
Learn how to use Dagger2 and Hilt for Dependency Injection in Android applications. Discover how these tools help manage dependencies efficiently, making your code more modular, testable, and maintainable.
What is Dependency Injection (DI)?
Dependency Injection (DI) is a design pattern used to implement Inversion of Control (IoC), where the control of creating objects and their dependencies is delegated to an external entity. This makes the code more modular, easier to test, and more maintainable by decoupling classes from their dependencies.
In Android, Dependency Injection is crucial because it allows you to inject dependencies like network clients, databases, or repositories into your activities, fragments, and view models without manually initializing them each time.
Two popular DI frameworks in Android are Dagger2 and Hilt. Hilt is built on top of Dagger2 but simplifies its setup and usage.
i) Using Dagger2 for Dependency Injection
Dagger2 is a fully static, compile-time dependency injection framework for Java and Android. It provides a robust and scalable way to manage dependencies in Android projects, but it requires more setup and boilerplate code compared to Hilt.
Steps to Use Dagger2
- Add Dependencies:
First, add the necessary dependencies to your build.gradle files.
- Create the Dependency Classes:
Let’s say you have a NetworkService class that you want to inject into your activity:
- Create a Module to Provide Dependencies:
A Module in Dagger2 provides methods that supply instances of your dependencies. Use the @Module annotation to define it and the @Provides annotation for each method that provides a dependency.
- Create a Component:
A Component is responsible for connecting the provided dependencies with the classes that need them. It acts as a bridge between your Module and your target class.
- Inject Dependencies:
Finally, inject the dependency into the target class (e.g., an Activity or Fragment) using @Inject and AppComponent.
Benefits of Using Dagger2:
- Compile-time checks for dependencies.
- Scalable and flexible, especially in large applications.
- Efficient dependency resolution and lifecycle management.
ii) Using Hilt for Dependency Injection
Hilt is a modern, simplified version of Dagger2 that integrates seamlessly with Android. It reduces the boilerplate code required for Dagger2 and is officially recommended by Google for dependency injection in Android apps.
Steps to Use Hilt
- Add Dependencies:
To use Hilt, add the following dependencies to your project’s build.gradle files.
In your root build.gradle:
In your app-level build.gradle:
- Create a NetworkService Class:
Create your dependency class just like with Dagger2.
- Create a Hilt Module (Optional):
With Hilt, you can optionally create modules to provide dependencies. But in many cases, Hilt uses constructor injection directly, so this step is not always necessary.
- Use @Inject for Constructor Injection:
With Hilt, the preferred method is constructor injection, where dependencies are automatically injected into classes through their constructors.
- Use @AndroidEntryPoint:
To allow Hilt to inject dependencies into activities, fragments, services, etc., mark them with @AndroidEntryPoint.
- Inject Dependencies in ViewModels:
Hilt also supports injecting dependencies into ViewModels.
Benefits of Using Hilt:
- Simpler than Dagger2, with reduced boilerplate code.
- Fully integrated with Android and recommended by Google.
- Supports constructor injection, which is cleaner and easier to maintain.
- Automatically handles lifecycle management of dependencies.
- Ideal for new Android projects.
Comparison – Dagger2 vs. Hilt
| FeatureDagger2Hilt | ||
| Setup Complexity | High, requires more boilerplate | Low, reduces boilerplate significantly |
| Integration | Requires manual setup for Android components | Seamlessly integrated with Android components |
| Dependency Injection | Based on components and modules | Uses constructor injection and automatic binding |
| Official Support | Google-supported but more complex | Official Google recommendation, simpler to use |
| Usage | Suitable for large, complex apps | Ideal for new apps or simple architectures |
Conclusion
In this tutorial, we explored Dependency Injection in Android using Dagger2 and Hilt. While Dagger2 is a powerful and scalable solution for dependency injection, it comes with significant complexity. Hilt, on the other hand, is designed to simplify the DI process by reducing boilerplate code and integrating more seamlessly with Android, making it the preferred choice for modern Android development.
By using DI frameworks like Dagger2 or Hilt, you can make your Android code more modular, testable, and maintainable, especially as your app grows in size and complexity.