'Hilt integration crashes application MainActivity_GeneratedInjector

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

   


Solution 1:[1]

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.

Solution 2:[2]

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
    }

Solution 3:[3]

  1. Removed the "@AndtoidEntryPoint" annotation from the activity.
  2. Ran the code. of course, it will fail.
  3. Add "@AndtoidEntryPoint" again
  4. Clean Project

Solution 4:[4]

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.

Solution 5:[5]

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

Solution 6:[6]

This is solved by invalidate cache and restart

Solution 7:[7]

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.

Solution 8:[8]

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'```
    

Solution 9:[9]

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

Solution 10:[10]

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

Solution 11:[11]

It's also happened to me when I'm importing an .aar with "implementation". (an .aar which includes hilt)

Like Sher Sanginov said instead of using "api" who increase compilation time, you can add the following lines in your build.gradle(app)

hilt {
    enableAggregatingTask = true
}

enableExperimentalClasspathAggregation is now deprecated,

see https://dagger.dev/hilt/gradle-setup#aggregating-task

Solution 12:[12]

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"

Solution 13:[13]

Steps need to perform:

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

Hopefully it will resolve your problem.