Domanda di colloquio di Compass Group

How does dependency injection work in Android? Explain how you would set up Hilt or Dagger in an Android project.

Risposta di colloquio

Anonimo

31 lug 2024

Dependency injection is a design pattern used to implement IoC (Inversion of Control) in Android. Hilt and Dagger are popular dependency injection frameworks in Android. They help manage object creation and dependency resolution, reducing boilerplate code and improving testability. To set up Hilt, you annotate your Application class with @HiltAndroidApp, your Activity or Fragment with @AndroidEntryPoint, and use @Inject to request dependencies in classes. Hilt simplifies the setup and usage compared to Dagger.