Dagger: Dagger Hilt doesn't work with Dynamic Feature Modules.

Created on 29 May 2020  路  10Comments  路  Source: google/dagger

Versions:

  • Dagger 2.28
  • Dagger Hilt 2.28-alpha

I tried Dagger Hilt with Dynamic Feature Modules.
Here is a sample project:
https://github.com/yamasa/Hilt_DFM

But, unfortunately, Dagger Hilt doesn't work for Activities in DFM.
It crashes with the following exception:

Caused by: java.lang.ClassCastException: com.example.hilt_dfm.DaggerMyApplication_HiltComponents_ApplicationC$ActivityRetainedCImpl$ActivityCImpl cannot be cast to com.example.hilt_dfm.feature.DfmActivity_GeneratedInjector
    at com.example.hilt_dfm.feature.Hilt_DfmActivity.inject(Hilt_DfmActivity.java:60)
    at com.example.hilt_dfm.feature.Hilt_DfmActivity.onCreate(Hilt_DfmActivity.java:35)
    at com.example.hilt_dfm.feature.DfmActivity.onCreate(DfmActivity.kt:17)
    at android.app.Activity.performCreate(Activity.java:7825)
    ...

Does Dagger Hilt currently support Dynamic Feature Modules?
If not, is there a plan to support it?

Most helpful comment

There's documentation on Hilt and DFM now at https://developer.android.com/training/dependency-injection/hilt-multi-module#dfm

In general though, because we're built off of subcomponents and monolithic components you won't be able to use the standard Hilt mechanisms like @AndroidEntryPoint with DFM.

All 10 comments

From https://dagger.dev/api/2.28/dagger/hilt/android/HiltAndroidApp.html

Annotation for marking the Application class where the Dagger components should be generated. Since all components will be built in the same compilation as the annotated application, all modules and entry points that should be installed in the component need to be transitive compilation dependencies of the annotated application.

Given that dynamic feature modules aren't dependencies of the application, seems like it's a no-go, at least for now.

Just FYI, we're going to come back to this issue about Hilt after the official launch. Thanks for filing it though and sorry for the wait!

Thank you. I'm really looking forward to it!

@yamasa can you try this? https://github.com/google/dagger/issues/970#issuecomment-565317632

I am not using it with dynamic features yet. But I am not implementing my data module in the app module (I have another module for the di stuff) and it is working for me (Android Gradle plugin 4.0.0 and kotlin 1.4-M2)

I tried and it is not working for fragments too

I think this is a design issue of Dagger Hilt.

Hilt uses a monolithic component system.
https://dagger.dev/hilt/monolithic
Therefore, all classes defined in components are referenced by the generated MyApplication_HiltComponents class.
I believe this is incompatible with DFM that loads classes dynamically.

There's documentation on Hilt and DFM now at https://developer.android.com/training/dependency-injection/hilt-multi-module#dfm

In general though, because we're built off of subcomponents and monolithic components you won't be able to use the standard Hilt mechanisms like @AndroidEntryPoint with DFM.

Hmmm, too bad.

@Chang-Eric
Can't we at least use @ViewModelInject and assisted SavedStateHandle with DFM?

Unfortunately, no. @ViewModelInject uses the Hilt ActivityRetainedComponent which is monolithic, so any @ViewModelInject class in your DFM won't be recognized.

Thanks for your comments.

In summary, I updated my sample project.
https://github.com/yamasa/Hilt_DFM

Was this page helpful?
0 / 5 - 0 ratings