tezvyn:

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

Curated by the Tezvyn teamSource: developer.android.combeginner
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 THIS TESTS: 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.

A GOOD ANSWER COVERS: 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.

COMMON WRONG ANSWERS: 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.

LIKELY FOLLOW-UPS: 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?

ONE 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.

Source: developer.android.com

Read the original → developer.android.com

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.

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