DI (Dependency injection) is a widely used technique in programming and ideally suited for Android development, providing dependencies to classes so that classes don’t have to create them themselves. By following the DI principles, you will be laying the foundation for good application architecture, greater code reuse, and easy testing. Have you tried manual dependency injection in your application? Even with many of today’s existing dependency injection libraries, as your project gets bigger, these libraries still require a lot of template code because you have to manually construct each class and its dependencies and create containers to reuse and manage the dependencies.

By following the DI principles, you will be laying the foundation for good application architecture, greater code reuse, and easy testing.

The new Hilt library defines a standard way to do DI in your application by providing a container for each Android class in your project and automatically managing its life cycle. Hilt is currently in alpha, please try it out in your application and give us feedback.

Hilt is built on top of the popular DI library Dagger and therefore benefits from Dagger’s compile-time correctness, runtime performance, scalability, and Android Studio support. If you need more information, please refer to “Dagger navigation has never been so simple | Android Studio 4.1”. As such, 74% of the top 10K apps in the Google Play Store make extensive use of Dagger. However, because the code is generated at compile time, the build time increases.

Since many of the Android Framework classes are instantiated by the operating system itself, there is template code associated with using a Dagger in an Android application. Unlike Dagger, Hilt integrates the Jetpack library with the classes in the Android Framework and removes most of the template code, allowing you to focus on defining and injecting the important parts of the binding without worrying about managing the Dagger configuration and associations. Hilt can automatically generate and provide the following:

  • A component to integrate the Android Framework class with the Dagger, avoiding manual creation
  • Hilt automatically generates scoped annotations for components
  • Predefined bindings and qualifiers

Most importantly, since the Dagger and Hilt can coexist, you can migrate the application as needed.

Hilt of actual combat

To show you the ease of use of Hilt, we’ll demonstrate some quick DI through a typical Android application. Let’s use Hilt to inject the AnalyticsAdapter into MainActivity.

First, add the @HiltAndroidApp annotation to your Application class to enable Hilt for your Application and trigger code generation for Hilt:

@HiltAndroidApp
class MyApplication : Application() {... }Copy the code

Second, accessorize the AnalyticsAdapter constructor with the @inject annotation, specifying how Hilt provides examples:

class AnalyticsAdapter @Inject constructor() {... }Copy the code

Third, to Inject the AnalyticsAdapter instance into the MainActivity, add the @AndroidEntryPoint annotation to the Activity to enable Hilt and add the @Inject annotation to the field to perform the injection:

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
  @Inject lateinit var analytics: AnalyticsAdapter
  override fun onCreate(savedInstanceState: Bundle?). {
    super.onCreate(savedInstanceState)
    // The analytics instance has been assigned by Hilt and is available for use}}Copy the code

For more information, check out the features of the new annotations in the end cheat sheet.

Support for Jetpack

You can easily use your favorite Jetpack library through Hilt. In this release, we support ViewModel and WorkManager direct injection.

For example, inject a component schema ViewMode — LoginViewModelm into the LoginActivity: Annotate LoginViewModel with @ViewModelInject, and then use it in your Activity or Fragment as you wish.

class LoginViewModel @ViewModelInject constructor(
  private val analyticsAdapter: AnalyticsAdapter
): ViewModel { ... }
@AndroidEntryPoint
class LoginActivity : AppCompatActivity() {
  private val loginViewModel: LoginViewModel by viewModels()
  override fun onCreate(savedInstanceState: Bundle?). {
    super.onCreate(savedInstanceState)
    // loginViewModel is ready to be used}}Copy the code

For more information on Jetpack support, check out: Official Android documentation | Hilt and Jetpack integration

Start using Hilt

If you’re already interested in Hilt and want to learn more, we’ve put together the following guide to help you figure out how to add Hilt to your Android application

The document

If you are a little new to DI or Dagger, check out the guide above to add Hilt to your Android app. If you already know about Dagger, check out the documentation provided in Dagger. Dev /hilt. If you just want to know about the new notes and what you can do with Hilt, check out and bookmark the cheat sheet at the end of this article.

Dagger user oriented

If you already use Dagger or Dagger. Android in your application, check the migration guide or Codelab mentioned below to help you switch to Hilt. Since Dagger and Hilt can coexist, you can migrate your application step by step.

Codelab

We have published the following two Codelabs to teach you how to use Hilt by hand:

  • Use Hilt in Android applications
  • Migrate the Dagger application to Hilt

The sample code

Would you like to see how Hilt is used in an existing application? Please refer to the following resources:

  • Making | Google I/O 2020 applications
  • Making | Android dev – hilt branch of architecture – samples warehouse

feedback

Hilt is currently in version 1.0.0-Beta01. If you have any problems with the use of Hilt, please feel free to let us know.

Cheat sheet

This cheat sheet gives you a quick look at the functional differences between Hilt and Dagger annotations and how to use them.