Dependency Injection with Hilt

Hilt is Google's opinionated dependency injection library for Android, built on Dagger. It wires your object graph at compile time so classes receive their dependencies instead of constructing them.

Learn Dependency Injection with Hilt in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end you'll set up @HiltAndroidApp , use @Inject constructors, and write a @Module .

What You'll Learn in This Lesson

1️⃣ Enabling Hilt

Annotate your Application with @HiltAndroidApp to create the root container, then mark each Android class that should receive dependencies with @AndroidEntryPoint .

These two annotations connect your Android entry points to the dependency graph Hilt generates.

2️⃣ @Inject Constructors

For classes you own, an @Inject constructor tells Hilt how to build them — and Hilt supplies their dependencies automatically. Add @Singleton for a single shared instance.

UserRepository never builds its own ApiService — Hilt hands it over.

3️⃣ Modules with @Provides / @Binds

When you can't annotate a constructor — interfaces, library types — a @Module supplies the instructions. @Provides runs a factory method; @Binds maps an interface to an implementation. @InstallIn picks the component and scope.

Bind an interface, inject it into a @HiltViewModel , and retrieve it with hiltViewModel() .

📋 Quick Reference — Hilt

Practice quiz

Which dependency injection framework is Hilt built on top of?

  • Dagger
  • Guice
  • Spring
  • Koin

Answer: Dagger. Hilt is a layer over Dagger that adds Android-specific, opinionated conventions to reduce boilerplate.

Which annotation goes on your Application subclass to enable Hilt?

  • @HiltApp
  • @EnableHilt
  • @HiltAndroidApp
  • @InjectApp

Answer: @HiltAndroidApp. @HiltAndroidApp triggers Hilt's code generation and creates the application-level dependency container.

Which annotation lets an Activity or Fragment receive injected dependencies?

  • @HiltView
  • @AndroidEntryPoint
  • @Inject
  • @Component

Answer: @AndroidEntryPoint. @AndroidEntryPoint marks an Android class (Activity, Fragment, Service) as a target for field/member injection.

When you cannot annotate a constructor (e.g. an interface or library type), what do you use?

  • A second Application class
  • Reflection at runtime
  • Nothing — Hilt guesses
  • A @Module with @Provides or @Binds

Answer: A @Module with @Provides or @Binds. A @Module supplies instructions: @Provides for a factory method, @Binds to map an interface to its implementation.

What is the difference between @Provides and @Binds?

  • @Provides runs a method body to create the instance; @Binds maps an interface to an implementation with no body
  • They are identical
  • @Provides only works for Singletons
  • @Binds is for primitives only

Answer: @Provides runs a method body to create the instance; @Binds maps an interface to an implementation with no body. @Provides executes a method to build the object; @Binds is an abstract mapping from an interface to a concrete type, which Hilt resolves more cheaply.

How do you tell Hilt how to construct a class you own?

  • Add it to a list in the Application
  • Annotate its constructor with @Inject
  • Implement an interface
  • Register it in the manifest

Answer: Annotate its constructor with @Inject. An @Inject constructor tells Hilt exactly how to build the class and what its dependencies are.

What does @InstallIn specify on a module?

  • The package name
  • The minimum SDK
  • The Gradle module
  • Which Hilt component (and thus lifecycle/scope) the bindings live in

Answer: Which Hilt component (and thus lifecycle/scope) the bindings live in. @InstallIn(SingletonComponent::class), for example, installs the bindings into a component, tying their lifetime to it.

Which scope makes a single shared instance live for the entire app?

  • @ViewModelScoped
  • @ActivityScoped
  • @Singleton
  • @FragmentScoped

Answer: @Singleton. @Singleton in the SingletonComponent yields one instance for the whole application lifetime.

How do you obtain a Hilt-injected ViewModel inside a composable?

  • viewModel()
  • hiltViewModel()
  • MyViewModel.get()
  • new MyViewModel()

Answer: hiltViewModel(). hiltViewModel() retrieves a ViewModel whose dependencies are provided by Hilt.

Which is described as a lighter-weight alternative to Hilt/Dagger?

  • Koin
  • Glide
  • OkHttp
  • Retrofit

Answer: Koin. Koin is a popular service-locator-style DI library that avoids annotation processing, trading some compile-time safety for simplicity.