Hilt and Dagger2 Annotations Explained

DENNIS ILUMA
2 min readJul 6, 2021

--

@HiltAndroidApp.

@HiltAndroidApp Kicks off Hilt code generation. This create a base component container that houses all dependencies. that sit at the top off your applocation. All other component inherit from it.
You Must annotate the Application class as shown below

@HiltAndroidApp
class MyApplication : Application() { ... }

@AndroidEntryPoint

@AndroidEntryPoint. Any class annotated with this will have access to all dependencies stored in the Component container generated by @HiltAndroidApp. Meaning, It grants the class annotated with it permission to use dependency stored in the hilt container.

@AndroidEntryPoint
class MyActivity : AppCompatActivity() { ... }

@Inject

When placed before a constructor as shown below means that the class is avialable to be used as a dependency. In this case, it is called a constructor Ijection. What is happening behind the seen is that an instance of the class “AnalyticsAdapter” is created and stored in the container created at compile time. Making the instance of this class avialable to be used anywhere annotated with @AndroidEntryPoint

class AnalyticsAdapter @Inject constructor( private val service: AnalyticsService) { … }

When @Inject is placed inside a class annotated with @AndroidEntryPoint, it is called a field Injection as shown below.

@AndroidEntryPoint
class MyActivity : AppCompatActivity() {
@Inject lateinit var adapter: AnalyticsAdapter
/*
* This means an object of AnalyticsAdapter is stored as "var adapter" thus we can use all methods and fields difined in AnalyticsAdapter class
*/

@Module

It is used to inject out classes that cannot be constructor injected. These are class that don’t have constructor or classes that are comming from a third party dependen e.g Retrofit for Api calls and Room for RoomDB. Below is an example of how to use a module.

NOTE: Using a module goes with @InstallIn() and @Provides

@InstallIn(SingletonComponent::class)
@Module
class AnalyticsModule { ... }
//See interpretation Below

@InstallIn(SingletonComponent::class)

This means we are installing or attaching the modue to the SingletonComponent Container which was created at the background when the @HiltAndroidApp annotation was used. That means this module will be avialable whereever the SingletonComponent scope which is throughout the application.

@InstallIn(SingletonComponent::class)@Module
class AnalyticsModule { ... }

@Provides

It addes a binding to objects that cannot be constructed inject as dsicusses ealier. In this case, the return object is what is used.

@InstallIn(SingletonComponent::class)
@Module
class AnalyticsModule {
@Providesfun providesAnalyticsService(
converterFactory: GsonConverterFactory
): AnalyticsService {
return Retrofit.Builder()
.baseUrl("https://example.com")
.addConverterFactory(converterFactory)
.build()
.create(AnalyticsService::class.java)
}
}

--

--

DENNIS ILUMA
DENNIS ILUMA

Written by DENNIS ILUMA

0 Followers

Software engineer. Majors in Java, AI, Frontend, DevOps & Cloud.

No responses yet