meor9zcu12
meor9zcu12

Reputation: 101

Android Hilt throw IllegalStateException even stated Custom Application in Manifest.xml

I have a few real devices and also tested with Android studio's emulator and did not find any similar crash.

My problem is that I published the app and received crash reports from real users.

Google Play Console Crash Report

Type java.lang.RuntimeException
...
Caused by: java.lang.IllegalStateException:
at dagger.hilt.android.internal.managers.ActivityComponentManager.createComponent (ActivityComponentManager.java:76)
at dagger.hilt.android.internal.managers.ActivityComponentManager.generatedComponent (ActivityComponentManager.java:66)
at MyApp.Hilt_FirstEntryPage.generatedComponent (Hilt_FirstEntryPage.java:45)
at MyApp.Hilt_FirstEntryPage.inject (Hilt_FirstEntryPage.java:67)
at MyApp.Hilt_FirstEntryPage$1.onContextAvailable (Hilt_FirstEntryPage.java:38)
at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable (ContextAwareHelper.java:99)
at androidx.activity.ComponentActivity.onCreate (ComponentActivity.java:322)
at androidx.fragment.app.FragmentActivity.onCreate (FragmentActivity.java:249)
at MyApp.FirstEntryPage.onCreate (FirstPage.java:68)
at android.app.Activity.performCreate (Activity.java:8207)
...

ActivityComponentManager.java:76

Hilt Activity must be attached to an @HiltAndroidApp Application. Did you forget to specify your Application's class name in your  
 manifest's <application />'s android:name attribute?

I did check other stack overflow questions, I already did add my custom application into Manifest and run successfully in my testing devices.

My app has only one module and only one application, and also some several ContentProvider, BroadcastReceiver, AppWidgetProvider, WorkManager, JobIntentService, not sure they could affect the normal initialization of application to lead the crash.

For the FirstEntryPage activity, I did not inject any things. It is very simple activity page just like Hello World example. Therefore there could not be any component conflict. In addition, FirstEntryPage activity can be opened by PendingIntent from BroadcastReceiver and also notification.

The previous version did not apply hilt, and this version is the first time to bring hilt to real users.

AndroidManifest.xml

<application
    android:name=".MyApplication"

MyApplication.java

@HiltAndroidApp
public final class MyApplication extends MultiDexApplication implements Configuration.Provider

FirstEntryPage.java

@AndroidEntryPoint
public final class FirstEntryPage extends FragmentActivity implements OnClickListener, DialogInterface.OnClickListener

build.gradle

dependencies {
   classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.1'
}

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

dependencies {
   implementation 'androidx.hilt:hilt-work:1.0.0'
   implementation 'com.google.dagger:hilt-android:2.38.1'
   kapt 'androidx.hilt:hilt-compiler:1.0.0'
   kapt 'com.google.dagger:hilt-compiler:2.38.1'
}

Upvotes: 2

Views: 2495

Answers (2)

meor9zcu12
meor9zcu12

Reputation: 101

After searching answer in the Internet, I found the answer in the dagger Github https://github.com/google/dagger/issues/2798. I also tested by myself and there is no more such crash report in my Google Play Console after related code changes.

Here is the short answer:
When using hilt, you cannot use Android Auto Backup (android:allowBackup="true" without android:backupAgent), otherwise your app will crash.

If your app needs backup, with hilt, you must use Key/Value Backup (Android Backup Service) instead of Android Auto Backup.

That is, add these (and with your implementation of MyBackupAgent) in your AndroidManifest.xml

  • android:allowBackup="true"
  • android:backupAgent="MyBackupAgent"
  • android:fullBackupOnly="false"

If you app does not need backup, with hilt, just set android:allowBackup="false" in your AndroidManifest.xml

Upvotes: 8

Kishan Mevada
Kishan Mevada

Reputation: 701

You also need to declare @AndroidEntryPoint in your parent activity of your FirstEntryPage Fragment Activity.

Upvotes: 1

Related Questions