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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.