Skip to content
tezvyn:

How do you inject UserRepository into a ViewModel with Hilt?

Source: developer.android.comEasyHow cards are made

How do you inject UserRepository into a ViewModel with Hilt?

Tests Hilt constructor injection for classes you own. Annotate UserRepository's constructor with @Inject so Hilt auto-provides it to consumers like a @HiltViewModel. Red flag: using a @Provides module for a class you control.

What's really being asked

Whether you know that Hilt can automatically provide dependencies for classes you own through constructor injection without writing a Dagger module. The interviewer wants to see if you understand the distinction between bind-time configuration for third-party types and the zero-boilerplate path for your own code. They also care whether you recognize that Hilt builds the dependency graph at compile time, so constructor injection is preferred over field injection for testability and clarity.

The full answer

First, annotate the UserRepository constructor with javax.inject.Inject so Hilt knows how to instantiate it and which dependencies it requires. Second, state that Hilt will recursively satisfy the ApiService dependency if ApiService is also injectable, either through its own Inject constructor or through a Provides method in a Module if it comes from an external library. Third, mention that consumers like a ViewModel simply declare UserRepository in their own Inject constructor, typically inside a class annotated with HiltViewModel, letting Hilt inject it automatically. Fourth, emphasize that no module is required for UserRepository itself because you control its source and can add the annotation directly, keeping the graph discoverable at compile time.

The mistakes people make

Writing a Module with a Provides method that manually constructs UserRepository. This is over-engineering for a class you own and adds unnecessary maintenance and indirection. Another red flag is suggesting field injection inside the ViewModel instead of constructor injection, which breaks immutability, complicates testing, and prevents final properties. Some candidates also forget that ApiService must itself be providable, leaving a gap in the dependency graph explanation. A fourth mistake is confusing Hilt with a service locator pattern by suggesting manual retrieval from a component.

What usually comes next

How would you provide UserRepository if it came from an external library you cannot annotate? When should you use Binds instead of Provides for interface implementations? How does scoping work, and would you annotate UserRepository with Singleton or ActivityRetainedScoped? What happens if ApiService requires a runtime dependency like a base URL or an API key? How do you handle multiple implementations of the same interface?

A concrete example

class UserRepository @Inject constructor(private val apiService: ApiService) and class MyViewModel @HiltViewModel constructor(private val userRepository: UserRepository) : ViewModel(). Hilt generates the factory code at compile time, so you never manually write new UserRepository in the ViewModel or a module. If ApiService were from Retrofit, you would use a NetworkModule with a Provides method for ApiService, but UserRepository still needs only the Inject annotation.

Interview question

You control the UserRepository source code and need to inject it into a HiltViewModel. Which approach follows Hilt best practices?

  • a.Add @Inject to a lateinit var property inside the ViewModel to receive UserRepository after creation.
  • b.Have the ViewModel manually retrieve UserRepository from the Hilt component at runtime.
  • c.Create a Dagger module with a @Provides method that manually constructs UserRepository and its dependencies.
  • d.Annotate UserRepository's constructor with @Inject and declare it in the ViewModel's constructor; no module is needed for UserRepository.Correct
Why?

Annotating UserRepository's constructor with @Inject lets Hilt automatically provide it to the ViewModel without a module because you control the source. Writing a @Provides module for a class you own is tempting but wrong because it adds unnecessary boilerplate and hides the graph from compile-time validation.

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

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

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