Dagger and Hilt navigation support in Android Studio

Manuel Vivo
Apr 23, 2020 · 4 min read

Last update: July 23rd, 2020

Have you ever got lost in a project trying to figure out from where a Dagger or Hilt dependency is being provided? Does it come from an @Inject constructor? Or maybe from an @Binds or @Provides method? Does it have a qualifier? It’s not an easy task…

🤔 What if you could know that and more with just one click? 🎯 Ask no more!

The latest version of Android Studio 4.1 (currently available in Beta) comes with new gutter icons that allows you to easily navigate between Dagger-related code: dependency producers and consumers, components, subcomponents, and modules! Also, you can find the same information in Find usages.

Hilt support has been added to Android Studio 4.2 (currently in Canary). Apart from the Dagger features listed above, you can also benefit from easy navigation for entry points.

Easy Dagger and Hilt dependency graph navigation in Android Studio

As you can see, navigating the Dagger graph of your Android app has never been easier! Knowing from exactly which provider method a dependency is coming is just one click away with the new support in Android Studio.

In action

Starting with Android Studio 4.1 Canary 7, you can see a new gutter icon in projects that use Dagger or Hilt:

New Dagger and Hilt gutter icons in Android Studio

The behavior of these actions are as follows:

  • Icon with arrow up -> where the type is provided (i.e. where dependencies come from)
  • Tree-shaped icon -> where the type is used as a dependency

Let’s see some examples of the new functionality using the Dagger branch (dev-dagger) of the architecture-samples GitHub sample.

Knowing where dependencies are coming from

Given a class that can be injected by Dagger, if you tap in the gutter icon with the arrow up of a dependency, you’ll navigate to the method that tells Dagger how to provide that type.

In the following example, TasksViewModel has a dependency on TasksRepository. Tapping on the gutter icon takes you to the @Binds methods in AppModuleBinds that provides TasksRepository:

Know where a dependency is coming from

Also works with qualifiers!

Given the above, if the dependency is provided using a qualifier, it will take you to exactly that provider method!

DefaultTasksRepository depends on a TasksDataSource provided with a qualifier. Tapping on the gutter icon takes you to the method in AppModule that provides that type with that qualifier:

It also works with qualifiers!

Where is this type being used as a dependency?

When you have a method that tells Dagger how to provide a dependency, you can click the gutter icon with the arrow down to navigate to where that dependency is used. If that dependency is used by more than one consumer, you can select the consumer you want to navigate to from a list.

In our project, DefaultTasksRepository is used by different ViewModels. Which ones? You can know it by tapping on the gutter icon of the provider method (@Binds in this case):

Know where a type is used as a dependency

Hilt entry points

When you’re at a Hilt entry point, the gutter action helps you navigate to where a dependency is coming from. To showcase this feature, we’ll use the interop branch of the migrating Dagger to Hilt codelab.

Navigate where a type comes from at an entry point

Find Usages

You can find the same relationships between your Dagger/Hilt code with the Find usages feature in Android Studio.

If you right-click on the bindRepository of the AppModuleBinds class and select Find usages, for example, you’ll see something similar to this:

Find usages about bindRepository

What are you waiting for to give it a try? Please, use it in your projects and give us feedback in this link. Hope you enjoy it!

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