Migrating Tokopedia Academy App From Dagger Android Into Hilt!

Yehezkiel L
Aug 14 · 5 min read
Image for post
Image for post
Dagger Hilt Android
Image for post
Image for post
Code reduce with hilt
Code reduce with hilt

Getting started with Hilt

First, add Hilt plugin in build.gradle root project.

buildscript {
...
dependencies {
...
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
}
}
...
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

android {
...
}

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

Migrating Application

Below is the Application class using Dagger Android, We need to override DaggerApplication() and implement the functions to tell Dagger about Application.

MovieApplication With Dagger 2
MovieApplication With Hilt
  • Activity
  • Fragment
  • View
  • Service
  • BroadcastReceiver

Migrating Inject View

If you’re using Dagger 2 or Dagger Android, your view like Activity/Fragment will have DaggerComponent.inject or override DaggerFragment() code.

MovieListFragment With Dagger 2

Migrating Module

Sometimes if you cannot provide constructor-injected. Like in this project, We need Retrofit instance. But Dagger still doesn’t know how to provide the Retrofit because Dagger can’t create the Retrofit object by itself, and Retrofit is an external library that Dagger doesn’t know. So We need to provide the Retrofit instance manually inside our @Module.

AppModule With Hilt
Image for post
Image for post
Old AppComponent.kt
Image for post
Image for post
source

Migrating View Model Injector Factory

Last but not least, this project uses ViewModel, and We think it’s widespread that many new apps use this too. Usually, if we use ViewModel we need to provide our ViewModel and ViewModelFactory with Dagger. This also has a problem with boilerplate code like :

ViewModelFactory With Dagger 2
MovieListModuleProvider With Dagger 2
Image for post
Image for post
Safely Delete MovieListModuleProvider.kt
Image for post
Image for post
Safely Delete ViewModelFactory.kt
  • WorkManager
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0alpha01'
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'

Conclusion

That’s all the migrating our project from Dagger Android into Hilt. From here, We have s conclusion and pros/cons with Android Hilt.

  1. Reduce more codes
  2. Reduce boilerplate
  3. Jetpack Integration
  4. It’s easy to migrate, especially if you already have Dagger in your legacy code
class LoginActivity : AppCompatActivity() {

@Inject
lateinit var loginAnalyticsAdapter: LoginAnalyticsAdapter

override fun onCreate(savedInstanceState: Bundle?) {
DaggerLoginComponent.builder()
.context(this)
.appDependencies(
EntryPointsAccessors.fromApplication(
applicationContext,
LoginModuleDependencies::class.java
)
)
.build()
.inject(this)

super.onCreate(savedInstanceState)
...
}
}
Image for post
Image for post

Tokopedia Engineering

Story from people who build Tokopedia

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store