OOZOU
Get in Touch
Back to Blog

Using Dagger Hilt In Android Applications

How to use dagger-hilt to improve your workflow when working on Android applications

November 14, 2020
3 min read
Using Dagger Hilt In Android Applications

I'll be assuming the reader has a solid grasp of the basic knowledge of kotlin, dagger and dagger-android.

The Current Situation With Dependency Injection

If you've been around long enough in android development, you've probably used tools like dagger and dagger-android to handle dependency injection in your applications. I'm pretty sure we are all too familiar with the amount of code we have to write to setup our dependencies. You've probably had to create an ApplicationComponent Interface, multiple dagger modules for your activities/fragments and yet you still needed to wire up factories so you can inject your view models. Apparently that is also not the last of your troubles, you also have to repeat all of this processes again in order to get any meaningful integration testing done. Clearly the dagger-android library can be begin to feel like a burden.

Why Use Hilt?

The dagger-hilt library addresses virtually all the problems listed in the above section. The following are some of the benefits of using hilt:

  • Reduced boilerplate
  • Decoupled build dependencies
  • Simplified configuration
  • Improved testing
  • Standardized components

Want to know more about the benefits of Hilt, checkout Why Hilt on Dagger Dev

Talk Is Cheap, Show Me The Code

To use Hilt, add the following build dependencies to the Android Gradle module’s build.gradle file:

apply plugin: 'dagger.hilt.android.plugin'

dependencies {
  implementation 'com.google.dagger:hilt-android:<VERSION>'
  kapt 'com.google.dagger:hilt-android-compiler:<VERSION>'
  // For instrumentation tests
  androidTestImplementation 'com.google.dagger:hilt-android-testing:2.28-alpha'
  kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'
  // For local unit tests
  testImplementation 'com.google.dagger:hilt-android-testing:2.28-alpha'
  kaptTest 'com.google.dagger:hilt-android-compiler:2.28-alpha'
}

kapt {
  correctErrorTypes true
}

Add the following to your project's build.gradle

buildscript {
  repositories {
    <em>// other repositories...</em>
    mavenCentral()
  }
  dependencies {
    <em>// other plugins...</em>
    classpath 'com.google.dagger:hilt-android-gradle-plugin:<version>'
  }
}

Hilt Implementation All apps using Hilt must contain an Application class annotated with @HiltAndroidApp. @HiltAndroidApp kicks off the code generation of the Hilt components and also generates a base class for your application that uses those generated components

@HiltAndroidApp
public final class MyApplication extends Application {}

To enable members injection in your activity, annotate your class with @AndroidEntryPoint.

@AndroidEntryPoint
public final class MainActivity extends MyBaseActivity {
  @Inject Foo foo; <em>// Bindings in SingletonComponent or ActivityComponent</em>

  @Override
  public void onCreate() {
    <em>// Injection happens in super.onCreate().</em>
    super.onCreate();

    <em>// Do something with foo ...</em>
  }
}

Using Hilt With ViewModels

Hilt includes extensions for providing classes from other Jetpack libraries. Hilt currently supports the following Jetpack components: ViewModels and WorkManager Add the following to app/build.gradle

dependencies {
  ...
  implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
  kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
}

Provide a ViewModel using the @ViewModelInject annotation in the ViewModel object's constructor. You must also annotate the SavedStateHandle dependency with @Assisted:

class ExampleViewModel @ViewModelInject constructor(
  private val repository: ExampleRepository,
  @Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel() {
  ...
}

Then, an activity or a fragment that is annotated with @AndroidEntryPoint can get the ViewModel instance as normal using ViewModelProvider or the by viewModels()

@AndroidEntryPoint
class ExampleActivity : AppCompatActivity() {
  private val exampleViewModel: ExampleViewModel by viewModels()
  ...
}

Hilt Modules

Hilt modules are standard Dagger modules that have an additional @InstallIn annotation that determines which Hilt component(s) to install the module into.

@Module
@InstallIn(SingletonComponent.class) <em>// Installs FooModule in the generate SingletonComponent.</em>
public final class FooModule {
  @Provides
  static Foo provideFoo() {...}
}

Hurray!!! You are ready to start using hilt in your android application. Personally, I felt the implementation of Hilt is easy when compared to dagger. If you feel the same after testing the above, please give it a try in your next project.

Related Articles

How Can I Design a Software: A Beginners Guide for Business Owners

How Can I Design a Software: A Beginners Guide for Business Owners

Designing software is an exciting yet complex process that involves creativity, logical thinking, and problem-solving skills.

Read More →
The Future of Edge Computing: What Businesses Need to Know

The Future of Edge Computing: What Businesses Need to Know

The digital landscape is evolving rapidly, and businesses must adapt to keep up with rising demands for speed, efficiency, and real-time processing.

Read More →
The Future of Retail: AI, AR, and Digital Transformation Trends

The Future of Retail: AI, AR, and Digital Transformation Trends

Retail is evolving at an unprecedented pace, driven by AI (Artificial Intelligence), AR (Augmented Reality), and digital transformation technologies, revolutionizing the retail industry. Traditional brick-and-mortar stores are no longer just about selling products—they are becoming smart, interactiv

Read More →

Have a Project in Mind?

Let's discuss how we can help bring your ideas to life with our expertise in web and mobile development.

Start a Conversationhello@oozou.com
OOZOU Logo

Bangkok · Singapore · Hong Kong

X
Facebook
Instagram
LinkedIn

Services

  • Web Development
  • AI Agents & Generative AI
  • Mobile App Development
  • Data Analytics & Engineering
  • UI/UX & Product Design
  • Digital Transformation

Company

  • About Us
  • Careers
  • Blog
  • Case Studies
  • Today I Learned
  • Contact

More

  • Industries
  • Partner Network
© 2026 OOZOU. All rights reserved.
Privacy PolicyCode of ConductABAC Policy