Hilt is a very opinionated framework for achieving dependency injection for your Android applications. Since its opinionated it comes with a standard way of integrating dependency injection and hence takes away lots of boilerplate code that usually comes with integrating Dagger to any project.
Hilt is built on top of Dagger and hence it comes with all the compile-time checks and runtime performances Dagger provides. And of course if you are comfortable and happy using your current Dagger setup you don't have to use Hilt.
Adding to your project
Enable Java 8 in your project
@HiltAndroidApp- All apps that use
Hiltshould have an
Applicationclass with the
@AndroidEntryPoint- Use this annotation to inject dependencies to other
Androidclasses like Activities, fragment, broadcast receivers etc
@HiltViewModel- for denoting a
@Inject- Use this annotation to obtain the required dependencies. Note that the injected
@Injectannotation is also used to define bindings to dependencies ie annotate constructors of the dependencies that can be constructor-injected with the
@Injectannotation to let
Hiltknow how to provide that dependency.
@Module- Modules enables you to provide bindings for dependencies that cannot be constructor-injected mainly implementation of interfaces, or class from an external library etc.
@InstallIn- Additional annotation for Modules so that
Hiltknows which all Android classes each module will be used in. See below for some generated components that we can use
@Binds- This function annotation helps
Hiltto bind which implementations to be injected in case of interfaces. The annotation function's return type denotes the interface type and the function parameter denotes which specific implementation to use.
@Provides- This function annotation helps
Hiltto provide dependencies of classes from an external library or ones built with a builder pattern. The function return type denotes the type of the dependency that is provided, the function parameters denotes any other dependencies required to build the provided type, the function body tells
Hilthow to construct the particular dependency. Note that the function body is executed every time a dependency of that type is provided.
@Qualifier- Qualifiers are used when you want to provide different implementations of the same type.
Predefined qualifiers in Hilt
Hilt provides some predefined qualifiers for context namely
Generated components for Android
Hilt provides some generated components that can be used to
InstallIn dependencies from your
Module. They are:
SingletonComponent- injected for
ViewModelComponent- injected for
ActivityComponent- injected for
FragmentComponent- injected for
ViewComponent- injected for
ViewWithFragmentComponent- injected for
ServiceComponent- injected for
Note: There is no generated component for
Hiltinjects broadcast receivers from the
- Hilt only supports activities that extend
ComponentActivity, such as
- Hilt only supports fragments that extend
- Hilt does not support retained fragments
Check the resources below for more details about component lifecycles and scopes