Hilt Basics Interview Questions
ARCHITECTURE › Dependency Injection
What is dependency injection, and why use it on Android?
What a strong answer covers: DI means a class receives its dependencies from outside, usually through its constructor, instead of creating them itself. That makes classes easy to test with fakes, separates configuration from use, makes dependencies explicit and lets you control lifetimes such as singletons. Hilt standardises DI on Android on top of Dagger.
What are the key Hilt annotations, and what does each do?
What a strong answer covers: @HiltAndroidApp on Application starts code generation; @AndroidEntryPoint on Activities, Fragments, Views and Services enables injection; @HiltViewModel for ViewModels; @Inject on constructors (and fields); @Module with @InstallIn(Component::class) to provide things you can't construct-inject; @Provides for factory functions; @Binds to bind an interface to an implementation; @Singleton and other scopes.
What is the difference between @Provides and @Binds?
What a strong answer covers: @Binds is an abstract function mapping an interface to an implementation that can itself be constructor-injected; it generates less code. @Provides is a concrete function body that builds the object, used for third-party types like Retrofit or OkHttp, or when construction needs logic. They can't be in the same abstract module unless @Provides is in a companion object.
How do you inject an object whose parameters are only known at runtime, like a ViewModel that needs an item id?
What a strong answer covers: For navigation arguments, read them from SavedStateHandle, which Hilt provides to ViewModels. For other runtime values use assisted injection: @AssistedInject constructor(@Assisted id: String, repo: Repo) with an @AssistedFactory interface, and for ViewModels @HiltViewModel(assistedFactory = ...) and hiltViewModel<VM, Factory> { it.create(id) }.
Hilt vs Koin vs manual DI: how would you choose for a new app, and what are Hilt's real costs?
What a strong answer covers: Hilt checks the graph at compile time (errors surface as build failures, not crashes) and is Google's recommended option, but adds annotation processing (build time), generated code and a learning curve, and it's Android-only. Koin is a runtime service locator: easy, KMP-friendly, but errors appear at runtime. Manual DI is fine for small apps. A strong answer weighs team size, KMP plans, build time and testing.