Android Hilt (dependency injection)

Hilt is an Android Jetpack wrapper library to make dependency injection with Dagger easier and more efficient.

We have the following third-party dependency. With Dagger2 we would need to create a @Module, inject it into a @Component and create code into main to .built() it.

class DataSource {
	fun getRemoteData() {

Still, all classes that are going to be injected need to be marked as follows with @Inject and an empty constructor().

class DataSource @Inject constructor()

All activities and their fragments need to be marked with @AndroidEntryPoint.


Add this into your project’s root build.gradle file.

buildscript {
	dependencies {
		classpath ''

Then add this into your app’s build.gradle file.

apply plugin: ''

// check this is correctly set.
android {
	compileOptions {
		// since Hilt uses Java8 features, add this
		sourceCompatibility JavaVersion.VERSION_1_8
		targetCompatibility JavaVersion.VERSION_1_8

dependencies {
	implementation ""
	kapt ""

Then we need to annotate the our Application class.

class App: Application()

Now go to your AndroidManifest.xml and set this attribute android:name\=".App".


With Hilt we don’t need to explicitly create a @Component interface. It already has 7 such interfaces packed for us.

Delete the @Component class and use the following annotations instead.

Hilt Component Injector for
ApplicationComponent Application
ActivityRetainedComponent ViewModel
ActivityComponent Activity
FragmentComponent Fragment
ViewComponent View
ViewWithFragmentComponent View annotated with @WithFragmentBindings
ServiceComponent Service

When we use Dagger, we list all the modules belong to a Component, at the top of the Component. Now we include the component of a module at the top of the module instead.

class DataModule {
	fun providesDataSource(): DataSource{
		return DataSource()

Let’s switch to MainActivity. Remove the .inject part and add the new @AndroidEntryPoint annotation. This generates an individual Hilt component for each class marked with it.

class MainActivity: AppCompatActivity() {
	lateinit var dataSource: DataSource
	override fun onCreate(savedInstanceState: Bundle?) {
		// we may now remove this
		// (application as App).dataComponent.inject(this)

Remember to rebuild the project.