Change DI Library from Koin to Dagger-Hilt
M y first Android DI library was Koin. Dagger had a high running curve at the beginning and it was difficult to learn, and at that time, I used Koin to build an app at the company I work for. Recently, the Android studio unveiled a library called Dagger-Hilt, which makes it easier to use Dagger, which has a disadvantage of being difficult to learn. I didn’t know anything about the dagger, but I thought I should learn it someday, so I started using the Dagger-Hilt. Using HarryPotter, a sample project I used to posted about MVVM architecture, I changed my DI library from Koin to Dagger-Hilt to try to study about Hilt.
🧐 What is Dagger-Hilt?
The Hilt Library was first introduced at Dagger 2.28. It is currently in the alpha stage based on the post-creation date, and an official blog has been opened. Present, blog has a guidebook for easy migration using Dagger and a quick start guide for those who are new to Dagger-Hilt. First, let’s take a brief look at Hilt annotation.
@HiltAndroidApp
This annotation is the entry point for all modules that should be pasted into the application class we are going to use. It must be pasted to use Hilt, similar to @AndroidEntryPoint
, but this only works in an application class.
@AndroidEntryPoint
Annotation used for Activity, Fragment, View, Service, BroadCast Receiver, etc. It acts like @DaggerActivity
, @DaggerFragment
, instantiated and processed as the proper Hilt component at the right point in Android class and Lifecycle. The name of the base class is ‘Hilt_<annotated_class_name>’. You can then insert an object in super.onCreate() at this point using @inject
annotation.
@InstallIn
This is an annotation to add components to the module. It cannot be used without @Module
or @EntryPoint
annotation and cannot declare any components to add to this module.
Component
Currently, the Hilt components used by @InstallIn
are configured as follows: Each component has its each own name have lifetime. This means that the objects in the module follow the life cycle of each component.

@ViewModelInject
Simply paste this annotation into the ViewModel and you can automatically inject it. At this time, the ViewModel constructor can take advantage of all the dependency available through the @Activity RetainedComponent
scope, or can selectively take advantage of the @Assisted
annotation to use the SavedStateHandle
.
Now, let’s look through the code.
🥇 Koin
The following code shows the implementation using the existing Koin.
DataSourceModule
— Provides a data source that serves to import the required data after receiving an API service injection.
NetWorkModule
— Sets Retrofit and OkHttp and provides services.
ViewModelModule
— Receives the intent argument parameter and provides the viewModel via injection of the repository.
And that’s how Activity used it to hand over the required argument in ViewModel.
In the application class, the module was inserted into the startKoin
function and used.
🗡 Dagger-Hilt
Now let’s use Dagger-Hilt to change the code created using the Koin. First, let’s start with a build.gradle
file. Create the dependency in the project level build.gradle
file as follows
It also writes the module-level build.gradle
file as follows
From now on, Write code the modules in the same way. Please check it while comparing it with the code created by the Koin above.
DataSourceModule
NetworkModule
RepositoryModule
⚠️ Unlike the Koin, Dagger-Hilt does not require you to create a separate module for the ViewModel, and you can inject dependencies by attaching the ViewModelInject
annotation to the ViewModel.
Assisted
annotation allows you to inject a SavedStateHandle
to easily load and use the required intent argument.
And this is how you use it in Activity.
Finally, for the Application class, you just have to add an annotation called HiltAndroidApp
.
Conclusion
So I tried to use DI library with Koin and Dagger-Hilt easily, but in my opinion, the Dagger-Hilt is still in alpha stage, but I didn’t think there were many references and it was too early to use it at production level. Nevertheless, the official version will be released soon, so I think you good learn beforehand.
The full code can be found here. If this posting was helped, please give a star at GitHub.