Hilt Simple Example (Comparison with Dagger too)

iamVariable
4 min readDec 21, 2022

Hi Coders,

As we all know, we have to write so many boilerplate codes for Dagger in android to do Dependency Injection. We’re here to show you how easy to work with Hilt and how it works with full and simple example.

  1. First we start with Dagger in simple application.

Project structure:

Project structure

Create a simple empty android application.

1.1 Add below dependencies for Dagger

implementation 'com.google.dagger:dagger:2.44.2'
kapt 'com.google.dagger:dagger-compiler:2.44.2'

1.2 Create a DataSource class

package com.iamvaraiable.daggersimpleapp

import android.util.Log


class DataSource {
fun getUserData() {
Log.i("mmm ", "User data retrieved")
}
}

1.3 Create a Module Class to provide the data

package com.iamvaraiable.daggersimpleapp

import dagger.Module
import dagger.Provides

@Module
class DataModule {

@Provides
fun providesDataSource():DataSource {
return DataSource()
}
}

1.4 Create a Component class to inject the modules

import dagger.Component


@Component(modules = [DataModule::class])
interface DataComponent {

fun inject(mainActivity: MainActivity)
}

finally we have to use this component class in our application class

1.5 Create an Application class

import android.app.Application

class App:Application() {
lateinit var dataComponent: DataComponent

override fun onCreate() {
dataComponent = DaggerDataComponent.builder().build()
super.onCreate()
}
}

and make sure you gonna use this application class in your AndroidManifest.xml file

1.6 Finally in your MainActivity

package com.iamvaraiable.daggersimpleapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.iamvariable.daggersimpleapp.R
import javax.inject.Inject

class MainActivity : AppCompatActivity() {

@Inject
lateinit var dataSource: DataSource
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

(application as App).dataComponent.inject(this)

dataSource.getUserData()
}
}

if you run the app now you can see the log which we written in the DataSource class *Log.i(“mmm “, “User data retrieved”)*

Log you written in DataSource

Now we gonna achieve same using Hilt library. Let’s see how easy to use Hilt.

2. Hilt (Modifying the same above application using Hilt)

The first thing we have to add Hilt Dependency

add in your project level gradle

id("com.google.dagger.hilt.android") version "2.44" apply false

add below In your app level gradle

plugins {
...
id("com.google.dagger.hilt.android")
}

dependencies {
implementation("com.google.dagger:hilt-android:2.44")
kapt("com.google.dagger:hilt-android-compiler:2.44")
}

kapt {
correctErrorTypes = true
}

Project Structure

2.1 Application class changes

Remove all boilerplate codes and just add @HiltAndroidApp

package com.iamvaraiable.daggersimpleapp

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class App : Application()

2.2 Create a DataSource class(No changes)

package com.iamvaraiable.daggersimpleapp

import android.util.Log

class DataSource {
fun getUserData() {
Log.i("mmm ", "User data retrieved")
}
}

2.3 Create a Module Class to provide the data

As we removed boilerplate codes in application class so we just want to mention here to take care of the work by adding just @InstallIn(SingletonComponent::class)

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

@Module
@InstallIn(SingletonComponent::class)
class DataModule {

@Provides
fun providesDataSource():DataSource {
return DataSource()
}
}

2.4 MainActivity

Just to add @AndroidEntryPoint to capture the injection

package com.iamvaraiable.daggersimpleapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.iamvariable.daggersimpleapp.R
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

@Inject
lateinit var dataSource: DataSource
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
dataSource.getUserData()
}
}

Thats it. If you run the app now with Hilt changes you will get the same result.

Generated components for Android classes

For each Android class in which you can perform field injection, there’s an associated Hilt component that you can refer to in the @InstallIn annotation. Each Hilt component is responsible for injecting its bindings into the corresponding Android class.

The previous examples demonstrated the use of ActivityComponent in Hilt modules.

Hilt provides the following components:

In Addition,

Component lifetimes

Hilt automatically creates and destroys instances of generated component classes following the lifecycle of the corresponding Android classes.

Happie Coding:)

--

--

iamVariable

Experienced Mobile Application developer and in full software development lifecycle, including analysis, design, development, deployment.