Hilt
Android Development with Compose: Architecture & Data
Course 5 ยท Chapter 10 ยท Letting a Library Do the Wiring
Chapter 9 hand-wrote an AppContainer and a ViewModel factory. Hilt is Android's dependency injection library: you mark what exists and what is needed with annotations, and Hilt generates the container and the factories at build time. Everything from Chapter 9 still applies: constructor injection, fakes in tests. Hilt just removes the typing.
ksp configuration; the Hilt Gradle plugin is declared in the root build file with apply false and applied in each module, together with the KSP plugin; the Dagger docs listed Hilt 2.60.1 and the Android guide 2.57.1 (so versions here are placeholders); the docs name the compiler artifact hilt-android-compiler in one snippet and hilt-compiler in another, so check which your version uses;
hiltViewModel() retrieves a ViewModel scoped to the current navigation destination, with androidx.hilt:hilt-lifecycle-viewmodel-compose (1.3.0 in the guide); testing needs hilt-android-testing and compilers on the test configurations; and the module that compiles your Application class needs all Hilt modules and injected classes in its dependencies.
The search did not cover the annotations in depth. @Provides, @Binds, @InstallIn, @Singleton, @HiltViewModel and @Inject as written below are general knowledge, apart from one older deprecated codelab that used SingletonComponent. All code is unrun (no Kotlin compiler on the machine used to write this); nothing was seen on your install.
What Hilt Replaces
| Chapter 9 (by hand) | Hilt |
|---|---|
DefaultAppContainer building each object | Modules with @Provides and @Binds, plus @Inject constructors |
NotesApp creating the container | @HiltAndroidApp on NotesApp |
by lazy for one instance | @Singleton |
AppViewModelProvider factory | @HiltViewModel and hiltViewModel() (docs: Hilt generates the factory) |
| Wiring mistakes found at first use | Missing bindings reported when the app is built (general knowledge) |
Setup
- (docs) The plugin is declared at the root with
apply falseand applied in each module; the compiler goes throughksp. Room in Chapter 4 already needed KSP, so the plugin is probably already there. - (docs) Older setups used
kapt, which needs extra settings; avoid it in a new project. - (docs, unresolved) Compiler artifact name:
hilt-compilerorhilt-android-compiler. Take the one the current Hilt setup page for your version shows. - Versions: take current ones from the Hilt release notes. The two docs pages above disagreed, which is a sign they are updated often.
The Entry Points
(general knowledge) @HiltAndroidApp tells Hilt to build the app-wide set of objects (the "container" of Chapter 9, generated). @AndroidEntryPoint lets an Activity (or Fragment) receive injected objects; here it also makes hiltViewModel() work for composables inside it.
A ViewModel Hilt Can Build
(docs) hiltViewModel() retrieves a ViewModel scoped to the current navigation destination, using the androidx.hilt Compose artifact. (general knowledge) The ViewModel's constructor now carries @Inject and the class @HiltViewModel; the factory, AppViewModelProvider and the default-argument viewModel(factory = ...) are all gone.
The package of hiltViewModel has moved over time, so use the IDE's import suggestion rather than trusting the line above.
Telling Hilt How to Build Things
Classes you own can say "Hilt, you may construct me" with an @Inject constructor:
(general knowledge) @Singleton means one instance for the whole app, the job by lazy did in Chapter 9. Hilt can build DefaultNotesRepository because it can build or provide each constructor parameter. For the rest, you write a module:
@Provides(general knowledge) is for objects you cannot put@Injecton: a Room database, a Retrofit service, a DataStore. The function body is the recipe; its parameters are more dependencies Hilt supplies.@Binds(general knowledge) says which implementation to use when someone asks for an interface:NotesRepositorygetsDefaultNotesRepository. This is how Chapter 5's interface choice becomes configuration, and where a test could swap in a fake.@InstallIn(SingletonComponent::class)(general knowledge, and in an older deprecated codelab) says these bindings live for the whole app.@ApplicationContext(general knowledge) asks Hilt for the applicationContext, which a long-lived object should use rather than an Activity.
That is the whole of DefaultAppContainer, moved into annotations. Delete AppContainer, DefaultAppContainer, AppViewModelProvider and the old NotesApp body.
Testing
(docs) Tests that use Hilt itself need hilt-android-testing and a compiler on the test configurations (kspTest, kspAndroidTest), plus a test Activity annotated with @AndroidEntryPoint. (general knowledge) But the ViewModel unit test from Chapters 5 and 9 does not change: it builds the ViewModel directly with fakes. That is the benefit of constructor injection first.
Manual DI or Hilt?
| Manual (Chapter 9) | Hilt | |
|---|---|---|
| Code to write | A container and a factory that grow with the app | Annotations and a few modules |
| Magic | None: you can read every line | Generated code; errors can be harder to read (general knowledge) |
| Setup | None | Plugin, KSP, versions to keep in step |
| Fits | Small apps (docs: manual DI or Hilt for simple apps) | Apps with many classes and screens; the Catalogue project course uses it |
There is no right answer for a notes app. The honest summary of the docs is that both are acceptable for simple apps, and Hilt earns its setup as the dependency graph grows.
hiltViewModel() in Compose in place of by viewModels(), and KSP in place of kapt.
Try It Yourself
Convert the app from Chapter 9's container to Hilt: add the plugins and dependencies, annotate NotesApp, MainActivity and the ViewModel, write the modules, and delete the container and factory. Report the Hilt, KSP and compiler artifact names and versions you used, and any build error.
Provide the Json instance from a module and use it in provideNotesApi. Then bind a second interface (the TimeProvider from Chapter 9, Exercise 2) with @Binds. Explain when you need @Provides and when @Binds is enough.
Break it on purpose: remove the @Binds function, then restore it and remove @AndroidEntryPoint from MainActivity. Write down what each error says and whether it appeared at build time or at run time. Then say which approach, Chapter 9's or Hilt, you would choose for this app, and why.
ksp configuration; the root apply false plugin declaration applied in each module; versions 2.60.1 (Dagger docs) and 2.57.1 (Android guide); the compiler artifact name discrepancy; hiltViewModel() scoped to a navigation destination and the androidx.hilt Compose artifact; Hilt generating the ViewModel factory; the testing artifacts and configurations; the module that compiles Application needing all Hilt modules in its dependencies; kapt needing extra settings.General knowledge, not checked:
@HiltAndroidApp, @AndroidEntryPoint, @HiltViewModel, @Inject, @Singleton, @Provides, @Binds, @ApplicationContext and @InstallIn as written (only SingletonComponent appeared in an old deprecated codelab); the hiltViewModel import package; build-time reporting of missing bindings; the comparison table.Not verified: every listing and solution (unrun); the exercises (not run by me). Nothing was checked on your install.
Sources: Dependency injection with Hilt (developer.android.com), Use Hilt with other Jetpack libraries (developer.android.com), Hilt Gradle build setup (dagger.dev), Dagger KSP (dagger.dev), Hilt testing guide (developer.android.com), Hilt releases (developer.android.com), Using Hilt in your Android app codelab (developer.android.com, deprecated).