Illustration by Virginia Poltrack

Dependency injection on Android with Hilt

Jetpack’s recommended library for DI

Manuel Vivo
Jun 10, 2020 · 4 min read

Dependency injection (DI) is a technique widely used in programming and well suited to Android development, where dependencies are provided to a class instead of creating them itself. By following DI principles, you lay the groundwork for good app architecture, greater code reusability, and ease of testing. Have you ever tried manual dependency injection in your app? Even with many of the existing dependency injection libraries today, it requires a lot of boilerplate code as your project becomes larger, since you have to construct every class and its dependencies by hand, and create containers to reuse and manage dependencies.

By following DI principles, you lay the groundwork for good app architecture, greater code reusability, and ease of testing.

The new Hilt library defines a standard way to do DI in your application by providing containers for every Android class in your project and managing their lifecycles automatically for you.

Hilt is built on top of the popular DI library Dagger so benefits from the compile time correctness, runtime performance, scalability, and Android Studio support that Dagger provides. Due to this, Dagger’s seen great adoption on 30% of top 10k apps of the Google Play Store. However, because of the compile time code generation, expect a build time increase.

Since many Android framework classes are instantiated by the OS itself, there’s an associated boilerplate when using Dagger in Android apps. Unlike Dagger, Hilt is integrated with Jetpack libraries and Android framework classes and removes most of that boilerplate to let you focus on just the important parts of defining and injecting bindings without worrying about managing all of the Dagger setup and wiring. It automatically generates and provides:

  • Components for integrating Android framework classes with Dagger that you would otherwise need to create by hand.
  • Scope annotations for the components that Hilt generates automatically.
  • Predefined bindings and qualifiers.

Best of all, as Dagger and Hilt can coexist together, apps can be migrated on an as-needed basis.

Hilt in action

Just to show you how easy to use Hilt is, let’s perform some quick DI in a typical Android app. Let’s make Hilt inject an AnalyticsAdapter into our MainActivity.

First, enable Hilt in your app by annotating your application class with the @HiltAndroidApp to trigger Hilt’s code generation:

@HiltAndroidApp
class MyApplication : Application() { ... }

Second, tell Hilt how to provide instances of AnalyticsAdapter by annotating its constructor with @Inject:

class AnalyticsAdapter @Inject constructor() { ... }

And third, to inject an instance of AnalyticsAdapter into MainActivity, enable Hilt in the activity with the @AndroidEntryPoint annotation and perform field injection using the @Inject annotation:

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject lateinit var analytics: AnalyticsAdapter override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// analytics instance has been populated by Hilt
// and it's ready to be used
}
}

For more information, you can easily check out what the new annotations do in the cheat sheet section below.

Comes with Jetpack support!

You can use your favourite Jetpack libraries with Hilt out of the box. We provide direct injection support for ViewModel, WorkManager, Navigation, and Compose so far.

For example, to inject a Architecture Components ViewModel LoginViewModel into a LoginActivity: annotate LoginViewModel with @HiltViewModel, annotate the constructor with @Inject, and use it in an @AndroidEntryPoint activity or fragment as you’d expect:

@HiltViewModel
class LoginViewModel @Inject 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
}
}

Learn more about Jetpack support in the docs.

Start using Hilt

If you’re intrigued by Hilt and want to learn more about it, here’s some resources for you to learn in the way you prefer:

Getting started with Hilt

Learn how to add Hilt in your Android app with this guide.

Documentation

If you’re new to DI or Dagger, check out our guide to add Hilt to an Android app. Alternatively, if you already know Dagger, we’re also providing documentation on dagger.dev.

If you’re just curious about the new annotations and what you can do with Hilt, check out this cheat sheet in the section below.

For Dagger users

If you’re already using Dagger or dagger.android in your app, check out this migration guide or the codelab mentioned below to help you switch to Hilt. As Dagger and Hilt can coexist together, you can migrate your app incrementally.

Codelabs

To learn Hilt in a step-by-step approach, we just released two codelabs:

Sample code

Do you want to see how Hilt is used in existing apps? Go check its usage in the Google I/O 2020 app and in the dev-hilt branch of the Android architecture-samples Github repository.

Cheat sheet

This cheat sheet allows you to quickly see what the different Hilt and Dagger annotations do and how to use them.

Download cheat sheet in PDF

Hilt and Dagger annotations cheat sheet. Download in PDF here.

Android Developers

The official Android Developers publication on Medium

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store