Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

What is dependency injection in Android, and why use Hilt or Dagger?

Dependency injection means a class receives its dependencies from outside rather than constructing them. Instead of a ViewModel creating its own Repository, which creates its own API client, the dependencies are supplied.

Why it matters on Android specifically:

  • Testability. A ViewModel that constructs a real network client cannot be unit tested. One that receives a Repository interface can be given a fake. This is the main argument.
  • Lifecycle-correct scoping. Some objects should be singletons for the app's life — a database, an OkHttp client — while others should live only as long as a screen. A DI framework enforces that rather than leaving it to static fields, which leak.
  • Less boilerplate. Without it, every object graph must be wired by hand, and changing a constructor means editing every call site.

Hilt is built on Dagger and is the recommended option for Android. It generates the code at compile time, so errors surface as build failures rather than runtime crashes, and it has no reflection overhead. It provides predefined components matching Android lifecycles — SingletonComponent, ViewModelComponent, ActivityComponent — with annotations such as @HiltAndroidApp, @AndroidEntryPoint, and @Inject.

Note: Koin is the common alternative — simpler to learn and Kotlin-idiomatic, but it resolves at runtime, so a missing binding is a crash rather than a compile error. That trade-off is the honest comparison to draw.

All Android interview questions

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as