05/08/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 constructor? Or maybe from an or 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 methods in AppModuleBinds that provides TasksRepository: