Dagger 2: What does @Module(includes =) do?

12,829

See the docs for Module.includes:

Additional @Module-annotated classes from which this module is composed. The de-duplicated contributions of the modules in includes, and of their inclusions recursively, are all contributed to the object graph.

In short, yes, Module.includes is a way of adding a dependency on the listed modules (AddonModule, VersionModule, and AboutModule in your example) from anything that includes the enclosing Module (CoreModule in your example).

Share:
12,829

Related videos on Youtube

MattBoothDev
Author by

MattBoothDev

Updated on July 13, 2022

Comments

  • MattBoothDev
    MattBoothDev almost 2 years

    I'm working on a project and I'm trying to make it as modular as possible.

    I'm trying to use the @Module(includes = {}) annotation to achieve my goals, and it's not working too well.

    I have a gradle module for all my Java code and within that each section has a module (AboutModule for the About page dependencies, for example). Then, for the whole "Core" gradle module, I have one Dagger 2 module called "CoreModule" which looks like this:

    @Module(includes = {
            AddonModule.class,
            VersionModule.class,
            AboutModule.class}
    )
    public class CoreModule {
    }
    

    Am I right in thinking that in the Android gradle module where all the app code goes, I should be able to include that module in a Component and that Component can then @Inject anything from all those modules listed in CoreModule?

    At the moment I'm getting compile errors from those Components suggesting that the files they're injecting to are "asking" for a totally different class that isn't in the module.

    For instance, I have a GitHubComponent that is injected into a GitHubActivity and also tries to inject, using the @Inject annotation, GithubService, but the compiler is throwing out errors that this class is trying to inject another class... That isn't mentioned anywhere in the target file. I've sjmplfied this to try and weed out the error but I'm not seeing where it's going wrong.

    I can't find any documentation on this aspect. What does the includes part actually do? Am I using it right and therefore the error is elsewhere?

  • MattBoothDev
    MattBoothDev about 7 years
    Thanks for this. I looked high and low for that on the Docs page (albeit on the bus on my commute home...) but couldn't find it. I've been using them in the right way, which is great. Thanks for helping clear that up! With regards ti my errors I've actually figured out this is because CoreModule includes all the modules within the Core library and some of them have dependencies that aren't included in the Components I was using (GitHubComponent, in this case - Version and Addon modules have another Retrofit repository dependency not provided in the GitHub Module). So that solves that one too!