Android Hilt Dependency Injection: Exploring Key Annotations

Android-World
2 min readDec 9, 2023

--

Photo by Farzad on Unsplash

In the context of Android development with Hilt, several key annotations play pivotal roles in simplifying and structuring dependency injection. Let’s delve into each of these annotations and their specific functions:

1. @Module

  • Purpose: The @Module annotation identifies a class as a Hilt module. A Hilt module is a collection of methods that provide dependencies. It’s akin to a factory that creates objects needed for DI.
  • Usage: You typically use @Module in a class where you define methods annotated with @Provides. Each @Provides method is a factory for a particular dependency.
  • Example:
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Provides
fun provideRetrofit(): Retrofit {
// Configure and return Retrofit
}
}

2. @InstallIn

  • Purpose: @InstallIn specifies the Hilt component(s) a module will be installed in. Hilt components are containers that manage the lifecycle and scope of the provided objects.
  • Usage: Used in conjunction with @Module, it defines where the dependencies provided by the module will live and how long they will be stored in memory.
  • Example:
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
// ...
}

3. @AndroidEntryPoint

  • Purpose: This annotation is used to mark Android classes (such as Activity, Fragment, View, Service, etc.) as entry points for Hilt dependency injection.
  • Usage: When you annotate an Android class with @AndroidEntryPoint, Hilt knows to inject dependencies into that class.
  • Example:
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject lateinit var retrofit: Retrofit
// ...
}

4. @Inject

  • Purpose: @Inject is used to request a dependency. It can be placed on a constructor, a field, or a method.
  • Usage: In the context of Hilt, when @Inject is used on a constructor, it tells Hilt how to provide instances of that class. When used on a field or method, it tells Hilt to inject a dependency into that field or method.
  • Example:
class MyRepository @Inject constructor(private val apiService: ApiService) {
// ...
}

5. @HiltViewModel

  • Purpose: This annotation marks a ViewModel class to be setup for injection by Hilt.
  • Usage: It allows Hilt to inject dependencies into your ViewModel. The @HiltViewModel annotated ViewModel can then be retrieved in your activities or fragments using the standard ViewModel providers.
  • Example:
@HiltViewModel
class MyViewModel @Inject constructor(private val repository: MyRepository) : ViewModel() {
// ...
}

Conclusion

These annotations are the backbone of Hilt’s DI system in Android development. They provide a structured and intuitive way to manage and utilize dependencies across various components of an Android application. By understanding and correctly applying these annotations, developers can significantly streamline and enhance the maintainability of their Android projects.

If you enjoyed the article and would like to show your support, be sure to:👏 Applaud for the story (50 claps) to help this article get featured
👉Follow me on
Medium
👉Follow me on Twitter
Check out more content on my Medium profile

--

--

Android-World

Experienced Senior Android Developer with a passion for developing high-quality, user-friendly apps. https://twitter.com/MyAndroid_World