Hilt integration crashes application MainActivity_GeneratedInjector

Viewed 10223

Trying to integrate hilt using android Api Documentation but app crashed with following exception. https://developer.android.com/training/dependency-injection/hilt-android

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.package.application/com.package.application.MainActivity}: 

java.lang.ClassCastException: com.package.application.DaggerAppApplication_HiltComponents_ApplicationC$ActivityRetainedCImpl$ActivityCImpl 
cannot be cast to com.package.application.MainActivity_GeneratedInjector

   
14 Answers

What worked for me:

  1. Removed the @AndroidEntryPoint annotation from the activity.
  2. Ran the code and of course, it failed.
  3. Had a fragment inside the activity. So, I deleted the fragment code as well.
  4. Ran the code, it failed.
  5. Added the @AndroidEntryPoint annotation to the activity again.
  6. Ran the code. It ran and the error disappeared.

For me, as a dependency declaration inside my app build.gradle, i had to use api instead of implementation:

api project(':features:feedModule')

Learn more here: https://github.com/google/dagger/issues/2064

Update: Dagger team released classpath aggregation feature which can resolve this issue. You can learn more here: https://dagger.dev/hilt/gradle-setup#classpath-aggregation

Basically, in your app module build.gradle, you need to add:

hilt {
    enableExperimentalClasspathAggregation = true
}

If your Android Gradle plugin version used in the project is less than 7.0, make sure to also add this to you build.gradle file:

android {
    ...
    lintOptions {
        checkReleaseBuilds false
    }
  1. Removed the @AndroidEntryPoint annotation from the activity.
  2. Ran the code. of course, it will fail.
  3. Add @AndroidEntryPoint again
  4. Clean Project

In my case, I was getting a similar ClassCastException because the module where my @HiltAndroidApp app class was didn't have access to the module where my @AndroidEntryPoint activity was. Moving my App class to the app module (that has access to all the other Gradle modules) solved the problem.

From the docs:

Because Hilt's code generation needs access to all of the Gradle modules that use Hilt, the Gradle module that compiles your Application class also needs to have all of your Hilt modules and constructor-injected classes in its transitive dependencies.

Solved it by adding missing dependency for androidx hilt.

kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'

Other dependencies that I have in my gradle file.

implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02"
implementation 'com.google.dagger:hilt-android:2.28-alpha'
kapt 'com.google.dagger:hilt-android-compiler:2.28-alpha'
implementation "androidx.fragment:fragment-ktx:1.2.5"

Plus fragment and its activity who are injecting viewmodel using hilt both are annotated as @AndroidEntryPoint

I spent a couple of hours fixing this exact issue but solved it by doing Invalidate cache/restart + Rebuilding of all modules.

This is related to this issue from Hilt: Hilt: More meaningful error messages

Dagger sometimes doesn't work well, even if the code is correct.

One possible solution is to rename the class to something else("Class2"), rerun the app, and check if it works. If it works, rename the class back to its original.

This is solved by invalidate cache and restart

I had the same problem and here are the steps I followed:

  1. appModule starts with internal enter image description here

  2. In your app level build.gradle inside android section add the following line:

       enableExperimentalClasspathAggregation = true
    }
    
    
  3. My dependencies are in this order:

    
    classpath "com.google.dagger:hilt-android-gradle-plugin:2.40.5"
    
        app-level build.gradle: 
         top plugin section
    
     plugin{
      ...........
      id 'kotlin-kapt'
      id 'dagger.hilt.android.plugin'
     }
    
         dependencies section
         //dagger-hilt
     implementation 'com.google.dagger:hilt-android:2.40.5'
     kapt 'com.google.dagger:hilt-android-compiler:2.40.5'```
    

For me it was that I forgot to add @AndroidEntryPoint to a parent activity that was calling a second activity and this one calling the fragment. Obviously everyone of them need to have an @AndroidEntryPoint

It is really silly solution but just comment out the injection annotation and injected fields, run the app then uncomment them and run again

I was having the same java.lang.ClassCastException after adding Hilt. Steps to fix the problem:

1. Build -> Clean Project
2. Build -> Rebuild Project

Also kapt doesn't work any more. Just add one plugin:

id 'dagger.hilt.android.plugin'

Then, add Hilt dependencies:

implementation "com.google.dagger:hilt-android:2.38.1"
annotationProcessor "com.google.dagger:hilt-compiler:2.38.1"

Steps need to perform:

  • Remove .gradle file in app's directory
  • Invalidate cache and restart android studio

Hopefully it will resolve your problem.

Related