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 THIS TESTS: 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.
A GOOD ANSWER COVERS: 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.
COMMON WRONG ANSWERS: 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.
LIKELY FOLLOW-UPS: 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?
ONE 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.
Source: developer.android.com
Read the original → developer.android.com
- #android
- #hilt
- #dependency-injection
- #kotlin
- #viewmodel
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.