Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
yamasa opened this issue May 29, 2020 · 11 comments
Closed

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

yamasa opened this issue May 29, 2020 · 11 comments

Comments

@yamasa
Copy link

yamasa commented May 29, 2020

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?

@davidliu
Copy link

davidliu commented Jun 1, 2020

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
Copy link
Member

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
Copy link
Author

yamasa commented Jun 2, 2020

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

@JavierSegoviaCordoba
Copy link

@yamasa can you try this? #970 (comment)

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
Copy link

I tried and it is not working for fragments too

@yamasa
Copy link
Author

yamasa commented Jun 10, 2020

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
Copy link
Member

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
Copy link
Author

yamasa commented Jun 11, 2020

Hmmm, too bad.

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

@Chang-Eric
Copy link
Member

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

@yamasa
Copy link
Author

yamasa commented Jun 12, 2020

Thanks for your comments.

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

@gfpf
Copy link

gfpf commented Oct 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants