Exploring Jetpack Composes’ Dependencies Injection Using Hilt | by Aldo Surya Ongko | Aug, 2022

Tips to create loosely coupled, clean code

Photo by Ember Navarro on Unsplash

You can find the previous tutorials on Jetpack Compose below:

In this article, we will learn how to inject our application with Dependencies Injection using Dagger-Hilt in Jetpack Compose.

Photo by Markus Spiske on Unsplash

Dependencies injection is a design pattern that aims to separate the concern of constructing objects and using them by forming inversion of control, leading to loosely coupled programs.

In Android, the are two famous methods for Dependencies Injection. Koin and Dagger-Hilt. For more information about what to choose, you may refer here.

It does not matter whether to useDagger-Hilt or Koin, since they serve the same purpose, but in this article, I will useDagger-Hilt.

We have to set up Dagger-Hilt in our application. Open the build.gradle at the project level and copy this code:

Open build.gradle app level and copy this code:

Create JetpackComposeApp.kt and add the following code:

Go to AndroidManifest.xml and add this attribute inside the application tag:

That’s how we setup Dagger-Hilt in our application, as this is a mandatory step. Especially you need to make JetpackComposeApp.kt have @HiltComposeApp annotation to annotate the Application class, and make that Dagger-Hilt recognized in our application by defining android:name=”.JetpackComposeApp” in AndroidManifest.xml.

Now we can open MainActivity.kt and add the following code:

This annotation make Dagger-Hilt can inject dependencies to MainActivity.kt. Noted that we can place @AndroidEntryPoint to MainActivity.kt because MainActivity.kt is the place that has fragments such as HomeFragment.kt and DetailFragment.kt. Each of the fragments is related to ViewModel and Model in MVVM.

In the MVVM Model section, we will make modules in Dagger-Hilt to add some service functions. Create AppModules.kt and add this code:

From this, AppModules.kt marked as @Module class in Dagger-Hilt. Furthermore, This class will be installed at the application level using SingletonComponent. So this class will be executed first and will continue to exist as long as the application is on.

That way, we can implement service to AppModules.kt which will separate tightly coupled code that will be explained later.

Open GamesService.kt and GamesRepositoryImpl.kt. Look at the code below:

Each time the GamesRepositoryImpl.kt is called, it will initialize GamesServices.getInstance(). And that makes GamesRepositoryImpl.kt become tightly coupled to GamesServices.kt. To avoid it, move the service code to AppModules.kt as shown below:

Now AppModules.kt will provide the service using retrofit, and will inject the service into the repository as stated in the code above.

Because the service and the implementation is already been taken care from AppModule.kt, we will change GamesService.kt and GamesRepositoryImpl.kt to this:

Now GamesRepositoryImpl.kt will not become tightly coupled to GamesServices.kt. On top of that, GamesRepositoryImpl.kt have @Singleton annotation for Dagger-Hilt to know that this class is Singleton class.

For more information about Singleton, please refer to it here:

You can even inject String as well as this code below in AppModule.kt:

Noted that if we provide more than one same data type, we will use annotation @Named to differentiate in Dagger-Hilt.

So we only made some changes that will affect GamesRepositoryImpl.kt and GamesService.kt. So far, the code for both of them will look like this:

In ViewModel section, we just have to declare that ViewModel can be recognized as a Dagger-Hilt class like this. Open HomeViewModel.kt and DetailViewModel.kt, and add the following:

Now, ViewModel is declared as Dagger-Hilt ViewModel class.

Now all the left is in the View Section. Just open HomeFragment.kt and DetailFragment.kt and change this line:

Now, rebuild the app, and start running it. Your app will run just like before. But the code itself will be loosely coupled and cleaner too.

Here is the downside: Anytime you want to change the class that’s related to Dagger-Hilt, you must rebuild it. Otherwise, it will not run as expected.

We have learned much about how to inject our application with Dependencies Injection using Dagger-Hilt in Jetpack Compose.

All code for this article can be viewed here.

In the next article, I will write about how to divide our features using modularization in our application.

News Credit

%d bloggers like this: