Dagger Hilt: Simplifying Dependency Injection in Android

Reza Ramesh
3 min readJul 4, 2023

--

Dagger Hilt is a dependency injection library for Android that simplifies the whole process and reduces the unnecessary steps without losing any of the features of the original Dagger. It provides a standard way to incorporate Dagger dependency injection into an Android application.

The goals of Hilt are:

  • To simplify Dagger-related infrastructure for Android apps.
  • To create a standard set of components and scopes to ease setup, readability/understanding, and code sharing between apps.
  • To provide an easy way to provision different bindings to various build types (e.g. testing, debug, or release).

Hilt works by code generating your Dagger setup code for you. This takes away most of the boilerplate of using Dagger and really just leaves the aspects of defining how to create objects and where to inject them. Hilt will generate the Dagger components and the code to automatically inject your Android classes (like activities and fragments) for you.

To use Hilt in your app, you need to follow these steps:

  • Add the Hilt dependencies and plugins to your project’s build.gradle files.
  • Annotate your Application class with @HiltAndroidApp to trigger Hilt’s code generation and create an application-level container.
  • Annotate your Android classes, such as Activity, Fragment, ViewModel, Service, etc., with @AndroidEntryPoint to enable field injection and access the application-level container.
  • Define your dependencies as classes or interfaces annotated with @Inject, @Binds, or @Provides, and group them into modules annotated with @Module and @InstallIn.
  • Use constructor injection or field injection to inject your dependencies into your classes.

For example, you can create a UserRepository class that depends on a UserService interface, and provide them using Hilt:

// A dependency that provides user data from a remote source
interface UserService {
fun getUser(id: String): User
}
// A class that depends on UserService
class UserRepository @Inject constructor(
private val userService: UserService
) {
fun getUser(id: String): User {
return userService.getUser(id)
}
}
// A module that provides UserService
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Provides
fun provideUserService(): UserService {
return Retrofit.Builder()
.baseUrl("https://example.com")
.build()
.create(UserService::class.java)
}
}

Then, you can inject UserRepository into your ViewModel using constructor injection:

@HiltViewModel
class UserViewModel @Inject constructor(
private val userRepository: UserRepository
) : ViewModel() {
    fun getUser(id: String): User {
return userRepository.getUser(id)
}
}

And you can inject UserViewModel into your Fragment using field injection:

@AndroidEntryPoint
class UserFragment : Fragment() {
    // Inject UserViewModel into the fragment
@Inject lateinit var userViewModel: UserViewModel
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Use UserViewModel to get user data
val user = userViewModel.getUser("123")
// Update UI with user data
...
}
}

If you want to learn more about Dagger Hilt and how to use it to simplify dependency injection in your app, you can check out the following resources:

Dagger Hilt is a powerful and easy-to-use library that makes dependency injection a breeze on Android. It lets you focus on what matters most: your app’s functionality and user experience. With Dagger Hilt skills, you can build better apps faster and easier. So what are you waiting for? Start injecting today!

RezaRamesh

--

--

Reza Ramesh
Reza Ramesh

Written by Reza Ramesh

I am an Android developer and UI/UX designer with 5 years of experience in creating engaging and user-friendly mobile applications

No responses yet