Tips to create loosely coupled, clean code
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.
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.
Dagger-Hilt. For more information about what to choose, you may refer here.
It does not matter whether to use
Koin, since they serve the same purpose, but in this article, I will use
We have to set up
Dagger-Hilt in our application. Open the
build.gradle at the project level and copy this code:
build.gradle app level and copy this code:
JetpackComposeApp.kt and add the following code:
AndroidManifest.xml and add this attribute inside the
That’s how we setup
Dagger-Hilt in our application, as this is a mandatory step. Especially you need to make
@HiltComposeApp annotation to annotate the
Application class, and make that
Dagger-Hilt recognized in our application by defining
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
MainActivity.kt is the place that has fragments such as
DetailFragment.kt. Each of the fragments is related to
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:
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.
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:
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
GamesRepositoryImpl.kt to this:
GamesRepositoryImpl.kt will not become tightly coupled to
GamesServices.kt. On top of that,
@Singleton annotation for
Dagger-Hilt to know that this class is
For more information about
Singleton, please refer to it here:
You can even inject
String as well as this code below in
Noted that if we provide more than one same data type, we will use annotation
@Named to differentiate in
So we only made some changes that will affect
GamesService.kt. So far, the code for both of them will look like this:
ViewModel section, we just have to declare that
ViewModel can be recognized as a Dagger-Hilt class like this. Open
DetailViewModel.kt, and add the following:
ViewModel is declared as Dagger-Hilt
Now all the left is in the
View Section. Just open
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.