question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Split generated AssistedInject_*Module to multiple modules

See original GitHub issue

Currently the generated AssistedInject_*Module contains all the bindings for @AssistedInject.Factory annotated factories in one module. As a result, it’s impossible to have dependencies that exist only in subcomponents. For example:

I have two ViewModels, where one of them takes an argument (GithubApi in this case), which exists only in one the subcomponent hosting these view models.

class CommitDetailViewModel @AssistedInject constructor(
    @Assisted handle: SavedStateHandle,
    privatel val githubApi: GithubApi // Should be provided only in CommitDetailActivitySubcomponent
) : BaseViewModel(handle) {
    fun loadDetail() {
        Timber.v("Request commit detail")
        // use githubApi here
    }

    @AssistedInject.Factory
    interface Factory : ViewModelAssistedFactory<CommitDetailViewModel>
}

class CommitListViewModel @AssistedInject constructor(
    @Assisted handle: SavedStateHandle
) : BaseViewModel(handle) {

    fun loadCommits() {
        Timber.v("Request commit list")
    }

    @AssistedInject.Factory
    interface Factory : ViewModelAssistedFactory<CommitListViewModel>
}

interface ViewModelAssistedFactory<T : ViewModel> {
    fun create(handle: SavedStateHandle): T
}

@AssistedModule
@Module(includes = AssistedInject_ViewModelAssistedFactoriesModule.class)
public abstract class ViewModelAssistedFactoriesModule {
}

The generated AssistedInject_ViewModelAssistedFactoriesModule contains the bindings for both factories above. Due to that, I have to include it in the ApplicationComponent so it’s visible to my subcomponents. But then compilation fails since GithubApi’s binding is only defined in one of the subcomponents.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
davidliucommented, Jun 11, 2020

Like I mentioned, the AssistedModule doesn’t impose a dependency requirement. You can put it in each of your components, and it will be fine. Declaring the AssistedModule in your app component won’t require you to have the MainRepo at the app level; you just also need to have the AssistedModule at the MainActivity level. Just try it out in code instead of trying to figure it out beforehand.

I’m not sure how to be any more clear in my explanation so I just made an example project for this:

https://github.com/davidliu/MultiLevelAssistedModule/tree/master/app/src/main/java/com/deviange/multilevelassistedmodule

5reactions
JakeWhartoncommented, Dec 11, 2019

Today you can choose not to use @AssistedModule and specify the @Binds methods yourself in whichever modules you want. That’s not that far off from what you’re asking.

Ideally the @ContributesAndroidInjector mechanism would be generalized so we could have @ContributesAssistedFactory Foo.Factory foo() and it would do the right thing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Split generated AssistedInject_*Module to multiple modules #87
Currently the generated AssistedInject_*Module contains all the bindings for @AssistedInject.Factory annotated factories in one module.
Read more >
Splitting a module based on usage by other modules
First we'll tag the classes in C that are used by any classes in A or B. Select A and B (cmd-click to...
Read more >
FactoryModuleBuilder (Google Guice 5.0.1 API)
Provides a factory that combines the caller's arguments with injector-supplied values to construct objects. Defining a factory.
Read more >
Splitting large angular module into multiple modules inside ...
I am trying to split my large angular module into separate smaller modules. I can do this successfully and have the project compile...
Read more >
google-gin
ID Status Summary 201 New Move to Github Type‑Defect Priority‑Medium 178 Invalid GXT,GIN and GUICE Type‑Defect Priority‑Medium 166 Fixed Warnings on @Nullable Type‑Defect Priority‑Low
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found