I use Hilt and recently had a strange problem. For a limited number of devices, I encounter the following error.
*dagger.hilt.android.internal.modules.ApplicationContextModule.provideApplication
ApplicationContextModule.java, line 45
java.lang.ClassCastException: android.app.ContextImpl cannot be cast to
android.app.Application*
ApplicationContextModule
generated by Hilt:
/** Provides a binding for an Android BinderFragment Context. */
@Module
@InstallIn(ApplicationComponent.class)
public final class ApplicationContextModule {
private final Context applicationContext;
public ApplicationContextModule(Context applicationContext) {
this.applicationContext = applicationContext;
}
@Provides
@ApplicationContext
Context provideContext() {
return applicationContext;
}
@Provides
Application provideApplication() {
return (Application) applicationContext.getApplicationContext();
}
}
NOTICE:
ApplicationContextModule
was generated by Hilt
@HiltAndroidApp
class MyApp : Application(), Configuration.Provider {
...
}
What I am doing is just create an instance of RootWarninginViewModel
(which is annotated with @ViewModelInject
)
class RootWarningViewModel @ViewModelInject constructor(
...
) : ViewModel() {
This is what I am actually doing:
@AndroidEntryPoint
class RootWarningActivity : AppCompatActivity() {
private val viewModel: RootWarningViewModel by viewModels()
}
And here is the complete crash log:
***dagger.hilt.android.internal.modules.ApplicationContextModule.provideApplication ApplicationContextModule.java:45
dagger.hilt.android.internal.modules.ApplicationContextModule_ProvideApplicationFactory.provideApplication ApplicationContextModule_ProvideApplicationFactory.java:34
*.app.DaggerMyApp_HiltComponents_SingletonC$ActivityRetainedCImpl$ActivityCImpl.provideFactory DaggerMyApp_HiltComponents_SingletonC.java:6658
*.app.DaggerMyApp_HiltComponents_SingletonC$ActivityRetainedCImpl$ActivityCImpl.getActivityViewModelFactory DaggerMyApp_HiltComponents_SingletonC.java:6663
dagger.hilt.android.internal.lifecycle.DefaultViewModelFactories.getActivityFactory DefaultViewModelFactories.java:50
*.ui.splash.Hilt_RootWarningActivity.getDefaultViewModelProviderFactory Hilt_RootWarningActivity.java:61
-$$LambdaGroup$ks$bK8-QGfmFwPkSFGulNZ22qySm7s.a -.java:28
androidx.lifecycle.ViewModelLazy.getValue ViewModelLazy.java:52
androidx.lifecycle.ViewModelLazy.getValue ViewModelLazy.java:41
*.ui.splash.RootWarningActivity.getViewModel RootWarningActivity.java
*.ui.splash.RootWarningActivity.getViewModel RootWarningActivity.java:11
*.ui.base.BaseActivity.onCreate BaseActivity.java:74
*.ui.splash.Hilt_RootWarningActivity.onCreate Hilt_RootWarningActivity.java:31***
from Hilt can not cast ContextImpl to Application
No comments:
Post a Comment