17

Migrate from Dagger to Hilt — A Step by Step Guide

 2 years ago
source link: https://trinhlbk1991.medium.com/migrate-from-dagger-to-hilt-a-step-by-step-guide-ac249d0eac07
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Migrate from Dagger to Hilt — A Step by Step Guide

Photo by Emile Perron on Unsplash

In the previous blog post, I shared the guide to use Hilt to implement dependency injection in Android.

But there’s the fact that it’s not many chances for you to use Hilt in a project from the start. In most cases, you’ll have to migrate gradually from the legacy dependency injection library to Hilt. Also, there’s a high chance that dependency injection library is Dagger.

In this post, I’ll share a step-by-step guide to migrate from Dagger to Hilt gradually. Those were my personal experience when I tried to use Hilt in my side project — Buckist.

Also, I assume that you already have knowledge of Dagger and basic usage of Hilt.

1. Review the current project

Before doing any migrations, sit back and take an overall look at the current project.

The sample app is pretty simple, with 2 screens:

  • Login
  • Profile

All the data are mocked, and implementation is simplified to a minimum to save me from laziness and procrastination 🥲.

1*V_v_HqOs7hMVaHePMyP6Pw.gif?q=20
migrate-from-dagger-to-hilt-a-step-by-step-guide-ac249d0eac07

For the implementation details:

  • I apply a simple Clean Architecture approach.
  • Use Dagger for dependency injection (of course 😂) with AndroidInjection support.
1*YtqMcrNY2MNAhpneUMy8KQ.png?q=20
migrate-from-dagger-to-hilt-a-step-by-step-guide-ac249d0eac07

Also, because the main purpose of this post is migrating from Dagger to Hilt, I’ll visualize the Dagger components and modules relationship of this sample project for easier following:

1*D-hgP1kRdrrMhrDRP-eiQQ.png?q=20
migrate-from-dagger-to-hilt-a-step-by-step-guide-ac249d0eac07

2. Plan the migration

“A goal without a plan is just a wish.“ (Antoine de Saint-Exupéry)

After reviewing the current situation and before getting your hand dirty, you probably want to have a migration plan first.

What should we plan?

  • The component hierarchy equivalent: you must have a crystal clear idea of which component your app will map with which Hilt component.
  • The migration journey: which modules/components/activities/fragments should be migrated first or last. This depends on your particular project.

For this demo migration plan, the component hierarchy mapping is pretty straightforward:

  • AppComponent will map to SingleComponent in Hilt.
  • LoginFragmentModule and ProfileFragmentModule will be installed into FragmentComponent

Also, for the demo purpose, I’ll migrate the LoginFragment to Hilt first while keeping Dagger inProfileFragment to show you how to gradually migrating to Hilt piece by piece.

3. Migrate AppComponent Dependencies

The goal of this stage is to migrate all dependencies in AppComponent to SingletonComponent while keeping interop with legacy Dagger implementation everywhere else.

Install all the Modules into SingletonComponent

To do this, put the annotation @InstallIn(SingletonComponent::class) on top of the module class.

If you have too many modules, you can create an aggregator module that includes all the existing modules for temporary.

Remove existing AppComponent :

Replaced by AppAggregatorModule :

Update Application class

Now we can set up Hilt for the application class. Add the @HiltAndroidApp and remove the DaggerAppComponent.create().inject(this) inside the onCreate() and we’re good to go.

As you can see, we still keep the HasAndroidInjector implementation. Because we want to continue support Dagger while migrating to Hilt gradually.

After finishing the migration, you can go back and remove this code.

Now let’s rebuild the project 🔄


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK