Android Hilt Integration and Usage

Mohammad Farhan Habib
2 min readJul 19, 2021


I found Hilt simpler than Koin and is more close to Android-oriented implementations.


I was using Koin as dependency Injection before which is now replaced by Hilt, Here I will try to explain Hilt Usage as simpler as possible.


I found Hilt simpler than Koin and is more close to Android-oriented implementations.


Add the required Libraries from Android Hilt Documentation


Point 1

All apps that use Hilt must contain an Application class that is annotated with @HiltAndroidApp.

class ExampleApplication : Application() { ... }

Point 2

To provide dependencies to Android classes (Activity, Fragment, View, ViewModel…), use @HiltAndroidEntryPoint

class ExampleActivity : AppCompatActivity() { ... }

Point 3

For instance, If you want to inject AnalyticsAdapter class in Android classes to use it like this:

class ExampleActivity : AppCompatActivity() {
@Inject lateinit var analytics: AnalyticsAdapter

then you have to define Hilt bindings through constructor-injection for that AnalyticsAdapter class:

class AnalyticsAdapter @Inject constructor(
) { ... }

Point 4 (Important)

There is one limitation, you cannot contructor-inject an interface or class which you don’t own like Retrofit, Hilt provides Modules for this purpose, Let me explain you both cases and how to use it:

4.1 Interface Injection

For instance, If I have an interface ILogger and want to inject then there are 2 things I have to do,

1- Create abstract class which contains abstract function to return the interface:

abstract class UtilsModule {
abstract fun bindLogger(loggerImpl: Logger): ILogger

2- Contructor Inject the implementation of ILogger:

class LoggerImpl @Inject contructor() : ILogger {

Now Logger can be used anywhere throughout the Application since it has the SingletonComponent Scope:

lateinit var logger: ILogger

4.2 Inject the Class you don’t own

Constructor injection is also not possible if you don’t own the class because it comes from an external library (classes like Retrofit, OkHttpClient, or Room databases), or if instances must be created with the builder pattern, here you can inject an instance of AnalyticsService using @Provides annotation If you don’t directly own it:

object AnalyticsModule {
fun provideAnalyticsService(
// Potential dependencies of this type
): AnalyticsService {
return Retrofit.Builder()


I have just described the pattern here for quick usage, For further clarifications about Annotations you must check the Hilt Android Documentation.

You can also checkout my repository to review the practical demonstration:

Don’t forget to applaud if you like this article. Thanks

Please reach out to me in case of any query



Mohammad Farhan Habib

With 10+ years of experience in developing Android Mobile Apps, Restful APIs in Spring Boot and dapps using Ethereum Blockchain