Implementing Dagger HILT in Android Login Activity with Kotlin — Part 1

Arif Khan
4 min readAug 1, 2020

HILT is like a container that makes sure the dependencies that you declared in your code will automatically be resolved and injected into before executing the main logic. I assume that you know the concept already, so I will not explain the dependency injection in detail here. In this post I will show you how you can integrate HILT and use it in an android app, I will use the predefined login activity and make the required changes to inject the declared dependencies into it using HILT.

Prerequisites:

I assume that you’re familiar with Kotlin Programming language, OOP principles, MVVM development Pattern, and basics of Android development.

Note: At the time of writing of this article I have used Android Studio version 4.0.1

Let’s get started, create a fresh project with Login Activity.

Give it a name whatever you like to, in my case it is Learn Hilt DI, and choose Kotlin in the language dropdown and then click finish and let the Android Studio do its magic work.

This app by default follows the MVVM pattern.

Before moving further first let us identify existing dependencies in this current project code.

Dependency 1: In the LoginViewModel class it requires an instance of LoginRepository in the constructor, which is currently provided by the LoginViewModelFactory which is acting sort of a dependency injector for the LoginViewModel.

Dependency 2: In the LoginRepository class, it requires an instance of LoginDataSource in the constructor. Again the LoginViewModelFactory is responsible for creating an instance of LoginDataSource of LoginRespository class.

Dependency 3: In LoginActivity class, it requires an instance of LoginViewModel and it is provided by the LoginViewModelFactory.

So as we can see here LoginViewModelFactory is responsible for providing the required instances in the above-mentioned classes hence it is acting like a dependency injector. Now by introducing HILT we will remove the LoginViewModelFactory and introduce a better approach for the same.

Before moving further run your app once and make sure everything is working fine.

Now install HILT in your app

  1. First, add the hilt-android-gradle-plugin plugin to your project's root build.gradle

2. Apply the Gradle plugin and add these dependencies in your app/build.gradle file:

Add the following lines at the top of the file:
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

Add following dependencies to dependencies object:

implementation "com.google.dagger:hilt-android:2.28-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"

3. Add the following additional dependencies to your app/build.gradle file in the dependencies object.:

implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'

4. Finally, sync your project and run your app or build the project to make sure everything is working fine.

Now to introduce HILT into your application create a class BaseApplication in your app and annotate it with “@HiltAndroidApp” and extend this class from android.app.Application.

BaseApplication

Update the name attribute of the application in your project’s manifest file:

AndroidManifest.xml

Now open LoginActivity class and annotate it with “@AndroidEntryPoint” and update the following line

loginViewModel = ViewModelProviders.of(this, LoginViewModelFactory())
.get(LoginViewModel::class.java)

to the following line:

loginViewModel = ViewModelProvider(this).get(LoginViewModel::class.java)
LoginActivity

Now if you run your app it will through an exception don’t worry just follow along.

Open LoginViewModel and make the following changes:

  1. Add “@ViewModelInject” annotation
  2. Add constructor keyword
LoginViewModel

What we have done here is just let HILT know that this view model requires a dependency through constructor injection.

Now, create a package called modules, and in this create a Kotlin Object file ResposiotryModule.

RepositoryModule

Here we are telling HITL that RepositoryModule is a HILT module by annotating it by “@Module” and “@Intallin” tells HILT that it is available for injection in the entire application. RepositoryModule is a Kotlin object and it has a method that is responsible for creating a Singleton object of LoginRepository.

Now in a similar way create another module DataSourceModule it will provide the instance of LoginDataSource

DataSourceModule

Now delete LoginViewModelFactory file and run your app it should work as it was working before.

That’s all you have successfully integrated the HILT

What’s Next?

I will integrate the retrofit and room database in this application by using HILT.

Part 2: Implementing Dagger HILT in Android Login Activity with Kotlin Retrofit 2 Integration

I hope you enjoyed the article and learned something useful.

Thanks for reading.

--

--

Arif Khan

My specialties include quickly learning new skills and programming languages, problem-solving. Besides, I love circuit training 🤸 and weight lifting 🏋️