I am Never Giving Up

Today I was interviewed by my friends for selection in the formula bharat team, a team to compete in formula-style vehicles. I was prepared and nervous at the same time. I had my answer in mind, how…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Dagger and Hilt navigation support in Android Studio

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.

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.

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:

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

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!

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
Navigate where a type comes from at an entry point

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

Add a comment

Related posts:

Membuat Kolom Pencarian Pada Database

Sebelum menjelaskan cara pembuatan kolom pencarian pada database MySQL. Adapun langkah-langkah membuat database sederhana sudah saya jelaskan pada artikel lainnya atau bisa diklik halaman berikut…

Amazon is Getting Into Your Medicine Cabinet with Amazon Pharmacy

Amazon was already inching its way into healthcare with the Halo, but now they can deliver your prescriptions, too. They just recently announced the launch of Amazon Pharmacy. This isn’t their first…

Como instalar Nagios4 no CentOS7

Primeiro temos de começar então por fazer a instalação dos pré-requisitos, como o PHP 5.4, Apache 2.2 e o MySQL. Após os pré-requisitos instalados vamos usar os seguintes comandos para instalar os…