4

I try to make my feature module as a dynamic feature module while I am using Hilt as dependency injection framework. I receive following exception :

caused by: java.lang.ClassCastException: com.android.sample.viaplay.DaggerViaplayApplication_HiltComponents_SingletonC$ActivityRetainedCImpl$ActivityCImpl$FragmentCI cannot be cast to com.android.sample.viaplay.feature.list.ui.DashboardFragment_GeneratedInjector
        at com.android.sample.viaplay.feature.list.ui.Hilt_DashboardFragment.inject(Hilt_DashboardFragment.java:104)
        at com.android.sample.viaplay.feature.list.ui.Hilt_DashboardFragment.onAttach(Hilt_DashboardFragment.java:55)
        at androidx.fragment.app.Fragment.onAttach(Fragment.java:1783)
        at com.android.sample.viaplay.feature.list.ui.Hilt_DashboardFragment.onAttach(Hilt_DashboardFragment.java:43)

I have used Dagger and not Dagger-Hilt in another sample and it worked as expected : https://github.com/alirezaeiii/SampleDaggerRx

Is it possible to use Dynamic feature module with Hilt? If yes, how can I solve above exception. You can find my source code at : https://github.com/AliRezaeiii/Hilt-MultiModule-Cache

Ali
  • 9,800
  • 19
  • 72
  • 152

3 Answers3

3

As mentioned in the following link : Dagger Hilt doesn't work with Dynamic Feature Modules :

Hilt uses a monolithic component system which is incompatible with DFM that loads classes dynamically.

Ali
  • 9,800
  • 19
  • 72
  • 152
1

You can continue to use Hilt in your app when you have dynamic feature modules. Hilt currently provides some basic functionality to inject bindings into dynamic features, but ultimately you need to use Dagger to connect your shared module dependencies to your Hilt dependency graph and use Dagger multibindings to build a generic solution for your ViewModel injection.

Shawn Fetanat
  • 57
  • 1
  • 8
0

From Hilt in multi-module apps docs:

In feature modules, the way that modules usually depend on each other is inverted. Therefore, Hilt cannot process annotations in feature modules. You must use Dagger to perform dependency injection in your feature modules.

GFPF
  • 959
  • 14
  • 19