Need help with Dagger-Hilt-Playerground?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

199 Stars 68 Forks 34 Commits 0 Opened issues


A playground for learning dagger hilt on android

Services available


Need anything else?

Contributors list

# 25,370
33 commits

Dagger Hilt

Playground for learning how to use Hilt. A new way to incorporate Dagger dependency injection into an Android application.


Below is a list of examples.

Hilt Basics

I will be making video lectures using the gists below as examples.

  1. Preparing to use Hilt
    • Follow the setup procedure:
  2. Field injecting a class with no dependencies. You own that class.
  3. Scopes and the "tier-like" system
  4. Dependencies that require dependencies (Concrete classes that you own)
  5. Some things can't be constructor-injected. What is the solution?
  6. Hilt Modules, Binds and Provides
    • Teach in this order:
      1. ComplexMethod_VALID.kt
      2. ComplexMethod_INVALID.kt
      3. SimpleMethod.kt
  7. Multiple Bindings of the same type

Basic MVI Repository Pattern

NOTE This is not how I would build out the architecture normally. I greatly simplified this for beginners. See Simple Network & Cache Use-case for a Clean Architecture Implementation of the same thing.

  1. Retrieve data from with Retrofit
  2. Cache data with Room
  3. Display cached data in UI

Simple Network & Cache Use-case

  1. Retrieve data from with Retrofit
  2. Cache data with Room
  3. Display cached data in UI

Fragment Constructor Injection with Hilt

  1. How to do fragment constructor injection with Hilt.

Instrumentation Testing


Important points:

  1. Dependencies
    1. Hilt
      • androidTestImplementation "$hilt_version"
      • kaptAndroidTest "$hilt_version"
    2. launchFragmentInContainer
      • debugImplementation "androidx.fragment:fragment-testing:$fragment_version"
  2. Test classes
    • Annotate with
      . Example
    • Include
      . Example
  3. HiltTestApplication
    • Hilt automatically generates a
      . But it requires a custom Test Runner (see next point)
  4. Custom Test Runner
  5. Field Injection
    • Requres
      function of test class. Example
  6. Test Fakes
    1. Method 1: Replacing a Module
      1. Uninstall the module in the test class
        • @UninstallModules(AnalyticsModule::class)
          . Example
      2. Create a new module within the test class that replaces the old module. Example
    2. Method 2:
      • I can't get this to work...
        • @BindValue var myString: String = "gggf"
        • Might be a kotlin thing? Maybe can't change final params? Not sure.
  7. Custom Application for testing
    • Guide
    • I guess you might have to do this if one of your dependencies requires your custom Application class. But I think you could probably work-around this.
  8. Multiple Test rules
    • Guide
    • I didn't know this was an issue? Never encountered this.
  9. launchFragmentInContainer
    • launchFragmentInContainer
      does not work with Hilt because the activity it creates is not annotated with
      . So there is a work-around:
    • Guide
    • launchFragmentInHiltContainer
      1. Create
        • NOTE One short-coming of this work-around is you cannot specify a
          to the function. I made a slight modification so you can. That way you can do constructor injection. Modified HiltExt.kt
      2. Create /debug/HiltTestActivity.kt
      3. Create /debug/AndroidManifest.xml
      4. Launch the fragment:
        val scenario = launchFragmentInHiltContainer(
                                         factory = fragmentFactory


I got this weird error:

Could not determine the dependencies of task ':app:processDebugAndroidTestManifest'.
> Could not resolve all task dependencies for configuration ':app:debugAndroidTestRuntimeClasspath'.
   > Could not resolve{strictly 3.0.1}.
     Required by:
         project :app
      > Cannot find a version of '' that satisfies the version constraints: 
           Dependency path 'DaggerHiltPlayground:app:unspecified' --> 'androidx.test.espresso:espresso-core:3.2.0' --> ''
           Constraint path 'DaggerHiltPlayground:app:unspecified' --> '{strictly 3.0.1}' because of the following reason: debugRuntimeClasspath uses version 3.0.1
           Dependency path 'DaggerHiltPlayground:app:unspecified' --> '' --> ''
           Dependency path 'DaggerHiltPlayground:app:unspecified' --> '' --> ''
           Dependency path 'DaggerHiltPlayground:app:unspecified' --> '' --> '' --> ''


I just added this to

and everything worked fine: ``` android { ...
configurations.all {
    resolutionStrategy.force ''

} ```

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.