google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.42k stars 2.01k forks source link

Dagger Hilt doesn't work with Dynamic Feature Modules. #1865

Closed yamasa closed 4 years ago

yamasa commented 4 years ago

Versions:

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?

davidliu commented 4 years ago

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.

Chang-Eric commented 4 years ago

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!

yamasa commented 4 years ago

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

JavierSegoviaCordoba commented 4 years ago

@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)

JavierSegoviaCordoba commented 4 years ago

I tried and it is not working for fragments too

yamasa commented 4 years ago

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.

Chang-Eric commented 4 years ago

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.

yamasa commented 4 years ago

Hmmm, too bad.

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

Chang-Eric commented 4 years ago

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

yamasa commented 4 years ago

Thanks for your comments.

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

gfpf commented 19 hours ago

Hi there, I have a dynamic module called home that has a frag using @AndroidEntryPoint but I'm getting this error:

java.lang.ClassCastException: DaggerGranitexApp_HiltComponents_SingletonC$FragmentCImpl cannot be cast to HomeFragment_GeneratedInjector

Can't use Hilt in dynamic modules yet?

@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel, Any, Any, Any>(
    layoutId = R.layout.fragment_home
), IBaseView.NavigationInitializer {

merci merci