Hilt your Feature Module

Randy Arba
5 min readJun 11, 2021

--

Today I wanna start with dagger hilt, Dagger Hilt is a dependencies injection same as Dagger, but it more specialize for Android development, this is not a new thing that dagger in Android, we already knew that Dagger has integration also in Android but the implementation is same with general Dagger. Some people feel that still got problem and hard to develop it with Dagger and tried to find another alternative such as Koin, Kodein, etc.

Google announces Dagger Hilt already stable this year before that developer is initiated their self to experiment how Dagger Hilt integration. Then they felt that's good and comfortable to use it, better than general Dagger, because of that, Dagger Hilt become popular in Android community. it has many samples about that.

If you ask me, how do you think about Dagger Hilt, I said, hmmm I still didn’t know too much about that. I still learning Dagger hilt, but in general, I already use Dagger before, in my project not only in Android but all project that needs dependencies injection I will choose Dagger. So after a long time, I commit to learn Dagger Hilt into my project experiment, but not ordinary way I dare to take challenge so I try to migrate my experiment project from Kodein into Dagger Hilt, yes, another Kodein integration already learned last year, but now I try to migrate it.

When I tried to migrate it, I will choose which part that i should migrate first, oh, for your information, I use feature module and modularization in this project, so this project will migrate slowly one module into another module. this is the easiest way to scale your project just modularize and you get different environments, different modules, different config, it's like one feature module using Kodein, but in other modules using Dagger Hilt.

I start from App module first, let's add these dependencies into your build config in-app layer and root layer.

build.gradle.kts (app layer)
build.gradle.kts (root layer)

After those two already complete, then we should add dependencies in Feature module. I should highlight, why we need to add Dagger dependencies in feature module Gradle? Because last time i am thinking that all project that tied with App module with api(library) it will affect the other module also, the answer is yes, but not for Dagger or Library that create annotation. Dagger hilt need initiate separately into other modules that tries generate code(Code Generation), even the Dagger plugin also, if no, the annotation will not generate it into that module directory.

build.gradle.kts (Feature Module App)

HiltAndroidApp

Okay after all finish, we should re-sync Gradle to download all those dependencies, after that we will move into Android Code file, so first you should go to App, App file is Kotlin/Java file that extends Application class, it can inherit from that class also. We should add @HiltAndroidApp above class name below is example, don't forget to put App clas name in manifest file also.

App file in App Module
Module Injection file in App Module.

StoreDatModule File

Store module file, this file contains all dependencies that inject needed. Basically, it will provide the injection. So if we didn't put here, Dagger didn't know which object that created and inject

  • Contain all object that wanna be inject
  • All type is Singleton mean only one object references for all injection.
  • Please check all providers and injections if one of constructors not satisfied it will throw an error when build time.
Module Injection File in Feature Module

Component File

Hmmm, component file, why we still create component if we already using Hilt, but hilt said that they will get rid those process development. Yep thats true but not for multi module. Feature module is another exception. For multi module we using old way for implement Dagger. We need to create a component file and builder method. This component tied with App module dependencies and provide injection that already put in code via annotation. After component created re-build the project, the result will have a class component name with Dagger prefix, for example if the interface file name is StoreDataComponent become DaggerStoreDataComponent.

  • Call Module name from App module
  • Create Builder function
  • Create Injection into which screen it can be anything, activity, fragment, service, etc.
  • All return is StoreDataComponent data type.
Component File in Feature Module

Fragment File

Fragment file in a feature module, we already know that Hilt has special annotation is androidEntryPoint. This annotation made activity injection without initializing the Dagger component. So because we include App module dependencies we need to initialize as normal dagger implementation. Well that not supposed to learn Hilt then, if the result of integration like this, it's same what I did, when implementing general Dagger. but the special thing ia after we initialize that all Hilt annotation in feature project will active no need create second component, just created once component, so Hilt still useful in feature module after all. But the important is we should declare those components in activity or fragment, to get app module dependencies.

We need declare before call super in onCreate method, because the dependencies will be called before activity created.

  • Initialize Dagger Component file, need rebuild first.
  • No need to add AndroidEntryBuild Annotation
  • Inject will have references.
Fragment file in Feature Module

If you want to create a sample, you just create String data type provider in StoreDatModule, after that inject it into Fragment and call that injection in println() or Timber, please re-build each you create provider or add new related dagger configuration changes. Below is sample.

@Provide
@Singleton
fun provideString() = "This is String from Module" // create this from StoreDatModule add this into that class as function.
@Inject
lateinit var sampleString: String // call this in fragment
Timber.e("call injection $sampleString") // call this is onAttach

Thanks. This is another finding of Android Journey. Keep learning and experimenting.

--

--