Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From Dagger to Dagger Hilt

From Dagger to Dagger Hilt

## Abstract

During the 2019's Android Dev Summit conference, Google announced that it would stop the development of Dagger-Android and officially recommended to use pure Dagger 2 instead. Few times after those announces Dagger-Hilt was announced and presented as "a standard way to incorporate Dagger dependency injection into an Android application." On the 5th of May 2021 Dagger Hilt 1.0.0 was released.

I recently took the decision to give it a go as Dagger 2 is a powerful but yet difficult tool to understand and setup and did not really want to change the setup it already took me time to master.

I started using Hilt in a small internal app, a simple tool without the scope of an important production app. Once confident that Hilt would improve our production app I started the refactoring of the dependency injection code to follow Dagger-Hilt's template.

With this talk I'd like to share my experience with Dagger-Hilt with two integration examples:

* From scratch in a new project
* Refactoring of an important project already using Dagger 2

I will discuss pros and cons of Dagger-Hilt and what does it brings to the table.

## References

* Official Dagger Hilt documentation
* Android Developer - Dependency injection with Hilt

Robin Caroff

March 18, 2022
Tweet

More Decks by Robin Caroff

Other Decks in Programming

Transcript

  1. Context • 2019 Oct. - Android Dev Summit 2019 ◦

    Google recommends to use DI ◦ Dagger-Android discontinued ◦ Pure Dagger officially recommended by Google • 2020 June - Dagger Hilt 1.0.0 alpha • 2021 Mai - Dagger Hilt 1.0.0
  2. Hilt - Application class @HiltAndroidApp class MyApplication : Application() {

    @Inject lateinit var bar: Bar override fun onCreate() { super.onCreate() // Injection happens in super.onCreate() // Use bar } }
  3. Hilt - Activity class @AndroidEntryPoint class MyActivity : MyBaseActivity() {

    // Bindings in SingletonComponent or ActivityComponent @Inject lateinit var bar: Bar override fun onCreate(savedInstanceState: Bundle?) { // Injection happens in super.onCreate(). super.onCreate() // Do something with bar ... } }
  4. Hilt - Service class @AndroidEntryPoint class MyService : Service() {

    // Bindings in SingletonComponent or ServiceComponent @Inject lateinit var bar: Bar override fun onCreate() { // Injection happens in super.onCreate(). super.onCreate() // Do something with bar ... } }
  5. Hilt - Module @Module @InstallIn(ActivityComponent::class) object FooModule { // @InstallIn(ActivityComponent::class)

    module providers have access to // the Activity binding. @Provides @ActivityScoped fun bar(activity Activity): Bar {...} }
  6. Hilt - ViewModel @HiltViewModel class FooViewModel @Inject constructor( val handle:

    SavedStateHandle, val foo: Foo ) : ViewModel @AndroidEntryPoint class MyActivity : AppCompatActivity() { private val fooViewModel: FooViewModel by viewModels() }
  7. Hilt - Worker @HiltAndroidApp class MyApplication : Application(), Configuration.Provider {

    @Inject lateinit var workerFactory: HiltWorkerFactory override fun getWorkManagerConfiguration { Configuration.Builder() .setWorkerFactory(workerFactory) .build() } … }
  8. Hilt - Worker @HiltWorker class ExampleWorker @AssistedInject constructor( @Assisted appContext:

    Context, @Assisted workerParams: WorkerParameters, workerDependency: WorkerDependency ) : Worker(appContext, workerParams) { ... }
  9. Retrospective - My approach of migrating to Hilt • Small

    project integration from scratch ◦ Quick setup ◦ Less boilerplate code • Integration in main production app ◦ Refactoring ◦ Modules tied to standards components no more custom components ◦ Jetpack integrations ▪ ViewModels ▪ Workers
  10. Retrospective - My approach of migrating to Hilt • Small

    project integration from scratch ◦ Quick setup ◦ Less boilerplate code • Integration in main production app ◦ Refactoring ◦ Modules tied to standard components no more custom components ◦ Jetpack integrations ▪ ViewModels ▪ Workers
  11. Migrating to Hilt Vanilla Dagger & Dagger-Android are compatible with

    Hilt Migration guide: https://dagger.dev/hilt/migration-guide Hilt migration API
  12. Migrating to Hilt 1. Migrate the Application Component 2. Map

    custom components to Hilt’s components 3. Remove all custom components 4. Install custom modules in Hilt’s components 5. Update scopes 6. Build, run, test and deal with issues
  13. Main difficulties encountered Hilt fragments must be attached to Hilt

    activities Easy solution: Annotate all Activities with @AndroidEntryPoint even if not injected.
  14. Main difficulties encountered Default bindings are now the generic Application,

    Activity, etc. classes Not AppCompatActivity or your BaseActivity
  15. Take away - Hilt vs Vanilla Dagger Pros • Defines

    Android templates and conventions for Dagger • Less boilerplate code • Less space for mistakes compared to Dagger • Decent documentation • ViewModels and Workers injection simplified • Easier to onboard people new to Dagger Cons • Less customizable • Code less decoupled • No Fragments UI tests out of the box • Not suitable for Dynamic Features • Risk of additional build time overhead • More risks of being shut down
  16. Further reads Hilt dependency injection (video) - Android developers Dagger

    vs Hilt vs Koin vs Pure Dependency Injection - Vasiliy Zukanov Migrating to Dagger Hilt - Hilt’s official documentation Exploring Dagger Hilt an Introduction - Joe Birch Hilt in multi module apps - Android developers