Start a Project

Android Hilt

Hilt

Android Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project. It provides a standard way to use DI in your application by providing containers for every Android class in your project and managing their lifecycles automatically.

Dependency injection in Android

DI is a technique through which we can create required objects outside the class and provide those objects to a class. In big application project, classes are dependent of other classes. In programming, we achieve this connection between classes by creating objects. Dependency injection helps make these connections and enables you to swap out implementations for testing

Advantages of DI :

Adding dependencies

Add plugin in project’s root build.gradle

Add dependencies in your app/build.gradle file:

Hilt application class

All apps that use Hilt must contain an Application class that is annotated with @HiltAndroidApp. In Hilt, we don’t need to create a component, include every module, and build for generating DaggerAppComponent class. We can inject instance of app into other module in Hilt.

Hilt modules

A Hilt module is a class that is annotated with @Module. Like a Dagger module, it informs Hilt how to provide instances of certain types. You must annotate Hilt modules with @InstallIn to tell Hilt which Android class each module will be used or installed in.

Example : You wanna Hilt to inject that dependency into your Activity class.

To know more about Dependency Injection, check following link : Dependency Injection

 

Exit mobile version