Skip to content
tezvyn:

What is Dependency Injection and Hilt's benefit over manual instantiation?

Source: developer.android.comEasyHow cards are made

What is Dependency Injection and Hilt's benefit over manual instantiation?

Tests your grasp of inversion of control and why frameworks matter. A good answer defines DI as supplying dependencies from outside, states Hilt automates object graph creation and scoping, and notes manual instantiation scatters construction logic.

What's really being asked

Whether you understand Dependency Injection as an architectural pattern and can articulate why a compile-time framework like Hilt beats manual instantiation in a complex Android codebase. Interviewers want to see that you separate the general concept from the specific tool, and that you identify automatic object graph management, lifecycle-aware scoping, and compile-time safety as the primary wins rather than surface-level convenience.

The full answer

First, define DI concisely: a class receives its dependencies from an external source instead of creating them itself. Second, explain the manual instantiation problem: without a framework, every Activity or Fragment constructs its own dependencies, which duplicates construction logic, hides transitive dependencies, and tightly couples components to concrete implementations. Third, describe Hilt's specific benefit: it automates object graph construction at compile time via annotation processing and code generation, enforces scoping tied to Android lifecycles such as ActivityRetained or Singleton, and centralizes configuration in modules. Fourth, contrast the two approaches by noting that manual wiring scales poorly as the app grows because each consumer must know how to build its dependencies, while Hilt scales linearly because the framework resolves the entire graph automatically and fails at compile time if a dependency is missing.

The mistakes people make

Saying DI is only for unit testing or mocking. Claiming the main benefit is simply less code or fewer constructor arguments. Describing Hilt as runtime reflection rather than compile-time code generation. Arguing that manual instantiation gives more control without acknowledging the cost of boilerplate, lifecycle leaks, and the difficulty of swapping implementations in large teams.

What usually comes next

How does Hilt differ from Dagger or Koin under the hood? When would you define a custom component versus relying on predefined ones? How do you handle multi-binding, qualifiers, or providing interface implementations? What happens if you inject a ViewModel with the wrong scope, and how does Hilt integrate with the Android framework classes?

A concrete example

Imagine a Repository that needs a Retrofit service and a Room DAO. Manually, every ViewModel or Activity must know how to build the database, configure the HTTP client, and instantiate the repository. With Hilt, you annotate the Repository constructor with Inject, provide the DAO and service in a Module, and request the Repository in your ViewModel. Hilt generates the factory code so the Room database is created once per Application scope, the Retrofit service is reused, and the Repository is available wherever needed without any consumer knowing the construction details or transitive dependencies.

Interview question

What is the main architectural benefit of using Hilt over manual dependency instantiation in Android?

  • a.It mainly reduces boilerplate by removing constructor parameters from framework classes
  • b.It shifts dependency creation to external modules primarily to support unit testing
  • c.It automates object graph construction and lifecycle-aware scoping at compile timeCorrect
  • d.It uses runtime reflection to resolve and inject dependencies when the app launches
Why?

Hilt generates the object graph at compile time and manages lifecycle-aware scopes, so consumers receive dependencies without knowing how to construct them. Distractor B is wrong because Hilt uses compile-time annotation processing and code generation, not runtime reflection.

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