How to Convert Dagger-Android to Hilt

Nemanja Stamenovic
The Startup
Published in
3 min readJul 12, 2020

The new Hilt library is helping us to apply the Dependency inversion principle in the code, and structure it in a layered way. It defines a standard to do Dependency Injection by providing containers for every Android class in the project.

Hilt is created on top of the Android DI library Dagger, so it enjoys the scalability, runtime performance, and all the stuff that Dagger provides. It is also including managing lifecycles in the application automatically. Hilt library integrates Jetpack and Android framework classes. It removes most of the boilerplate code because the developer has bigger fish to fry - like defining and injecting bindings, but without managing the Dagger setup. Keep in mind that Hilt is still in alpha!

Note: Here I’ll focus on converting the dagger-android library to Hilt.

Our starting point will be build.gradle(:app) file - so, go back to the drawing board:

//Hilt
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"

// Hilt ViewModel extension
def hilt_jetpack_version = "1.0.0-alpha01"
implementation "androidx.hilt:hilt-lifecycle- viewmodel:$hilt_jetpack_version"
kapt "androidx.hilt:hilt-compiler:$hilt_jetpack_version"

// Hilt testing dependencies
androidTestImplementation "com.google.dagger:hilt-android-testing:$hilt_version"
kaptAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version"
kaptAndroidTest "androidx.hilt:hilt-compiler:$hilt_jetpack_version"

👉 In Application.kt file, we need to remove HasAndroidInjector extension, and add @HiltAndroidApp above the class name:

@HiltAndroidApp
class SIOTv2Application : Application() {...}

Apply @HiltAndroidApp annotation to Application class. Now Hilt generates Application Component - this is the central point of our global dependency injection setup.

👉 Remove Application/Presentation Component (with all the listed modules), which contain stuff like this:

@Singleton
@Component(
modules = [FirstModule::class,
SecondModule::class,
ActivityBuilder::class,
ViewModelFactoryModule::class,
ViewModelModule::class]
)

Hilt automatically generates:

  • Components for integrating Android framework classes
  • Pre-defined bindings and qualifiers
  • Scope annotations

👉 Add the Application Module class instead of Component(s), for example:

@InstallIn(ApplicationComponent::class)
@Module
object ApplicationModule {
@Singleton
@Provides
fun provideRetrofitService(): APInterface =
Builder()
.baseUrl(TestAPInterface.API_URL)
.addConverterFactory(
create(
Moshi.Builder()
.add(NullToEmptyStringAdapter)
.add(KotlinJsonAdapterFactory())
.build()
)
)
.build()
.create(TestAPInterface::class.java)

...
@Singleton
@Provides
fun providesTokenInterceptor(): TokenInterceptor {
return TokenInterceptor()
}
...

The Component also contains a reference to the Activity, Fragment, View, and Service components. When constructing the Application Component class, an ApplicationContext module is instantiated and bound to the component, giving global access to an application-level context if there is the @ApplicationContext annotation.

The Component will handle the injection of the Application class. For this, it uses a single generated member injector class for all the required members of the Application class. This is triggered when an external call to the inject function of the component is made.

  • The component contains the required access points for dependencies.
  • For dependencies that do not require construction declarations, these will be instantiated on-the-fly.

👉 Each Activity/Fragment must have @AndroidEntryPoint annotation on top.

👉 In each ViewModel replace @Inject annotation with @ViewModelInject.

So that’s it, after these changes your application will use the Hilt library instead of Dagger! But keep in mind that Dagger and Hilt can coexist together if your application has specific requirements/use cases. Hope you found this story interesting. Feel free to share your feedback and comments.

Thanks for reading & happy coding! 🙌

--

--