Hilt: Question, How to reuse a module and provide its bindings
See original GitHub issueWe are trying out Hilt for our production project, we have a use case that I’m not sure how Hilt would handle it This is our approach towards providing different bindidngs when resuing a module:
Note I simplified the code below our actual use case has lots of modules and using Qulifiers is not convenient
@Module
class WidgetListModule {
@Provides
@FragmentScope
fun provideCategoryViewModelFactory(
widgetListDataSource: WidgetListDataSource<*>, // Not provided anywhere, user should provide it
compositeDisposable: CompositeDisposable,
threads: Threads,
alak: Alak,
): ViewModelProvider.Factory {
return viewModelFactory {
WidgetListViewModel(
compositeDisposable = compositeDisposable,
dataSource = widgetListDataSource,
threads = threads,
alak = alak,
)
}
}
}
The module that includes this module
@Module(includes = [WidgetListModule::class])
class ManageModule {
@Provides
@FragmentScope
fun provideWidgetListDataSource(
api: ManagePostAPI
): WidgetListDataSource<*> {
return WidgetListGetDataSource(api::getPage)
}
}
@FragmentScope
@Subcomponent(modules = [ManageModule::class])
interface ManageFragmentInjector {
fun inject(manageFragment: ManageFragment)
}
And we have cases that the injector being used to inject a field is determined at run time like this:
override fun inject() {
if (args.source == "foo") {
mainActivityComponent.fooFragmentInjector()
.inject(this)
} else {
mainActivityComponent.barFragmentInjector()
.inject(this)
}
}
So my question is how can I resuse a module and provide its dependencies with hilt?
Issue Analytics
- State:
- Created 3 years ago
- Comments:7
Top Results From Across the Web
Dependency injection with Hilt | Android Developers
Installing a module into a component allows its bindings to be accessed as a dependency of other bindings in that component or in...
Read more >Understanding Dependency Injection with Hilt. - Nyame Bismark
@Provides is an annotation that tells Hilt how a certain binding should be provided during injections. @Module is an annotation that tells Hilt...
Read more >How can Hilt be used effectively in multi-module apps ...
Unfortunately, Hilt uses a monolithic approach currently. This means that your app module will have access to ALL your modules.
Read more >Hilt - Providing Retrofit Instance and Repository - YouTube
The BEST android courses in the world: https://codingwithmitch.com/In this video I show two different ways to provide objects as ...
Read more >Frequently Asked Questions - Dagger
Only after Dagger examines each module and still cannot find an appropriate binding does it then check for the presence of @Inject constructors....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For anyone coming to this issue, the solution provided by @bcorso works with a bit of adjustment, since the injection has not happened yet, you will get an
NPE
exception if you access thefooWidgetListdataSource
so you can’t have@Inject
annotation on it like@Inject FooWidgetListdataSource fooWidgetListdataSource;
instead, you can access your dependence with an entry point interfaceYeah, this is a case where it’s possible to do with Hilt but you will loose a bit of the compile-time guarantees for that binding.
One solution is to provide the implementation from each fragment via an interface and have the
@Provides
method delegate to the fragment (there’s a similar use case in the migration guide, here). For example: