Skip to content
tezvyn:
Menu

@HiltViewModel: Simplified ViewModel Injection

Source: developer.android.comMediumHow cards are made

@HiltViewModel: Simplified ViewModel Injection

@HiltViewModel automates creating ViewModels with dependencies. Instead of writing custom factories, just annotate the ViewModel and its constructor. It's the standard for injecting dependencies like repositories into ViewModels in a Hilt-powered Android app.

Why it exists

Manually creating Android ViewModels that have dependencies (like a repository or use case) requires writing a custom ViewModelProvider.Factory. This is boilerplate code that is repetitive, tedious, and error-prone. Hilt was designed to eliminate this manual setup.

The mental model

Think of @HiltViewModel as a contract with the Hilt dependency injection framework. You are telling Hilt: "You are responsible for creating this ViewModel. I will tell you what it needs in its constructor, and you figure out how to provide those things and build the necessary factory for me."

How it works

When you annotate a ViewModel class with @HiltViewModel, Hilt's code generator creates a special factory for it behind the scenes. You must also provide an @Inject-annotated constructor so Hilt knows what dependencies to supply. Then, in an Activity or Fragment annotated with @AndroidEntryPoint, you can request the ViewModel as usual (e.g., using the by viewModels() Kotlin property delegate). Hilt intercepts this request, uses its generated factory to create the ViewModel instance, and provides all the necessary dependencies from its dependency graph.

When to use it

Use @HiltViewModel for every ViewModel that has dependencies in an application that uses Hilt for dependency injection. It is the standard, idiomatic way to integrate ViewModels into the Hilt ecosystem.

When not to use it

Don't use it if your project does not use Hilt for dependency injection. In very rare, complex scenarios, you might need a custom factory for dynamic dependency creation that falls outside Hilt's standard capabilities, but this is highly uncommon. For nearly all cases in a Hilt project, @HiltViewModel is the correct choice.

One canonical example

To create a ProfileViewModel that depends on a UserRepository, you would define the ViewModel like this: class ProfileViewModel @Inject constructor(private val userRepository: UserRepository) : ViewModel() { ... }. You must annotate the class with @HiltViewModel. Then, in your Fragment, you annotate the Fragment class with @AndroidEntryPoint and get the ViewModel simply by declaring private val viewModel: ProfileViewModel by viewModels(). Hilt handles the injection of UserRepository automatically.

Interview question

Which statement accurately describes the primary role of the @HiltViewModel annotation in an Android application?

  • a.It directly injects all required dependencies into the ViewModel's fields at runtime.
  • b.It automatically generates the necessary ViewModelProvider.Factory for dependency injection.Correct
  • c.It makes the annotated ViewModel a singleton instance throughout the application lifecycle.
  • d.It ensures the ViewModel's state is automatically persisted across process death and recreation.
Why?

The @HiltViewModel annotation's primary role is to tell Hilt to generate the boilerplate ViewModelProvider.Factory, simplifying the injection of dependencies into ViewModels. While Hilt does inject dependencies, it does so via the constructor, and the annotation itself facilitates the factory creation, not direct field injection.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles