Android: Learn about Hilt

Tram Ho

Dependency injection (DI) is a widely used technology in programming and is suitable for programming Android where dependencies are provided for the class instead of creating them. By following the DI principles, we create a good foundation for our application with the ability to reuse large code and easily test. Have you ever tried manually injecting dependency code? Even with many dependency injection libraries available today, it requires a lot of code as the project gets bigger, because we have to build every class and its dependencies manually, and create containers to Use and management dependencies

The Hilt library defines a standard for implementing DI in your application by providing containers in every Android class in your project and automatically managing their lifecycle for you.

Hilt is built based on a famous DI library, Dagger, so it inherits the exact compile time, runtime performance, scalability and Android Studio support that Dagger provides. Because of this Dagger accounts for 74% of the top 10k apps in Google Play. However, due to compile-time code generation, the application build time increases

Because many android framework classes are created by the operating system itself. There should be a related summary when using Dagger in Android apps. Unlike Dagger, Hilt is integrated with Jetpack libraries and framework classes and eliminates most of the templates to allow you to focus on the critical parts of identifying and managing dependencies without having to worry about managing all setups and bridges of Dagger. It will automatically create and deliver

  • Components to integrate Android framework class with Dagger that you need to create manually
  • Scope annotations for the components are automatically created by Hilt
  • Predefined bindings and qualifiers.

Because Dagger and Hilt can coexist, we can easily migrate when needed

Hint in action

In this example, I will show you how easy Hilt is to use by injecting AnalyticsAdapter into MainActivity.

First, enable Hilt in your application with the HiltAndroidApp annotation that activates Hilt gen code

Second, tell Hilt how to provide the instance of AnalyticsAdapter by annotating with the constructor with Inject

To inject the AnalyticsAdapter instance into MainActivity, enable Hilt in the activity with the AndroidEntryPoint annotation and perform the injection using the Inject annotation.

Hilt with Jetpack support

You can use your favorite Jetpack libraries with Hilt. Hilt has provided direct injection support for ViewModel and WorkManager

For example, to inject LoginViewModel into LoginActivity: annotate LoginViewModel with ViewModelnject and use it in the desired activity and fragment.

Start using Hilt

To learn about hilt in Android app you can read this doc

If you’re new to DI or Dagger, check out this guide to add Hilt to Android apps

If you already use Dagger, you can migrate to Hilt with this doc

This is a project using Hilt: https://github.com/android/architecture-samples/tree/dev-hilt

And here are some annotations in Hilt

References: https://medium.com/androiddevelopers/dependency-injection-on-android-with-hilt-67b6031e62d

Share the news now

Source : Viblo