Hilt

Dependency Injection with Android

Introduction

Dependency injection is one of the ways to achieve Clean Architecture by abstracting dependencies and by doing so make the code less coupled, which means easier modularisation.

Google has helped to create open-source tools that allow an easier management of dependencies.

Dagger

Google has created dagger which is a dependency injection tool for java and kotlin.

Hilt

Hilt is a dependency injection library for Android apps, which under the hood uses Dagger, thus simplifying the setup and reducing boiler plate.

Guide

@Module

This annotation tells Hilt that this class contributes to dependency injection object graph.

\_@InstallInclass

This annotation tells Hilt that the dependencies provided via this module shall stay alive as long as application is running.

@Provides

This annotation marks the method provideChannelDao as a provider of ChannelDao.

Now Hilt knows how to create an instance of ChannelDao but to create such an instance, Hilt needs to know how to create an instance of AppDatabase. For that add another method below provideChannelDao.

@Provides  
@Singleton  
fun provideAppDatabase(@ApplicationContext appContext: Context): AppDatabase {  
    return Room.databaseBuilder(  
        appContext,  
        AppDatabase::class._java_,  
        "RssReader"  
    ).build()  
}

There are two things to note here.

  1. @Singleton annotation tells Hilt that AppDatabase should be initialized only once. And the same instance should be provided every time it’s needed to be injected.
  2. @ApplicationContext allows hilt to provide application context without having to explicitly specify how to obtain it.

HilAll Hilt apps must have an Application class that is annotated with @HiltAndroidApp .

@HiltAndroidApp
class MainActivity: Application(){

}

Relates to

Using room database with Hilt

References