Dagger.Hilt TLDR (updated for 2.31.2-alpha)

A very short tutorial on how to use Dagger.Hilt if you don’t have time to read through the docs.

TLDR 2 minutes

Add gradle dependencies:

Apply these two gradle plugins to your app/build.gradle

Create your own Application class, can be as simple as this:

Notice the @HiltAndroidApp annotation, which is necessary for the dependency injection to work.

Register your application class in your AndroidManifest.xml:

Annotate constructor of the dependency you want to inject using @Inject annotation:

Finally, inject the dependency to your activity or fragment:

Done! You can run your project, someDependency contains an instance of SomeDependency , that was injected for you by Dagger.Hilt. Notice the @AndroidEntryPoint annotation of the class, that you need to add to any Activity or Fragment you want to inject dependencies into.

I have a multi-module architecture and want to support dependency injection

Actually this is it. The dependency itself could come from any gradle module (provided you depend on the module in gradle).

TLDR 5 minutes

Ok, so you want to depend on an interface, not an actual implementation of it. What do you do?

Let’s change our SomeDependency class to implement some interface:

And change activity code so that it depends on the interface:

Ok, running this you’ll get:

You need to create a Dagger @Module, class that will tell Dagger, which implementation of SomeInterface to actually inject.

This is an example module:

The module needs to be annotated by @Module annotation, that comes from Dagger 2. This annotation means, that the class or interface annotated by it provides clues for Dagger, how to construct certain dependencies. For example how to construct instances of an interface, should some class depend on that interface.

The module also needs to be annotated by @InstallIn(SingletonComponent::class) . This annotation tells, that dependencies defined here can be accessed by any class in the app. You could install the module into some other @Component and thereby control which classes of your app have access to which dependencies, but this topic is outside of the scope for this tutorial and most projects probably won’t need it anyway. You can learn more in the docs.

I want to share the same instance of SomeInterface across the whole app

Ok, easy, just add @Singleton annotation in the @Binds method signature, like this:

Bonus: I want to inject ViewModel into my Activity/Fragment

Provided you already have added all relevant gradle dependencies, you’ll need to add some new Android Jetpack dependencies:

Now if you want to inject dependencies into your ViewModel, use the standard@Inject annotated constructor, like you would in other classes. On top of that annotate the whole class with @HiltViewModel :

Adding the ViewModel to activity/fragment is trivial:

The whole activity class:

If you’ve tried to use dependency injection with Dagger 2 on Jetpack ViewModel , you know how painful it was. Now it’s easy, using just this one annotation @ViewModel inject. The library takes care of the rest for you automatically.

But Dagger.Hilt is still in alpha!

It is. But it already works well. Being in alpha mostly means, that some APIs could change, but the simple use cases presented here are in my humble opinion unlikely to change. Personally, I’d recommend using Dagger.Hilt over Dagger.Android at this point, but should you for whatever reason decide to go with the bit older Dagger.Android and don’t know where to start, I’ve got you covered.

I’ve created a demo project and published it on Github, if you’d like to get hands on working implementation. You can view changes by commits, they roughly represent steps described in this article to integrate Dagger.Hilt to your app: https://github.com/lukas1/Dagger.Hilt.TLDR