Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 918
MattBoothDev Avatar asked May 03 '17 20:05

MattBoothDev


People also ask

What is @module in Dagger?

It defines a configuration point for your object graph, where you declare which objects you want to be available for injection and their scopes. As a simple example, let's say we want a singleton object to be used by any Activity in the app.

What does @singleton annotation do?

The @Singleton annotation is used to declare to Dagger that the provided object is to be only initialized only once during the entire lifecycle of the Component which uses that Module.

What are the components in Dagger 2?

Components are essentially the glue that holds everything together. They are a way of telling Dagger 2 what dependencies should be bundled together and made available to a given instance so they can be used. They provide a way for a class to request dependencies being injected through their @Inject annotation.

What does inject mean in Dagger?

With the @Inject annotation on the constructor, we instruct Dagger that an object of this class can be injected into other objects. Dagger automatically calls this constructor, if an instance of this class is requested.


1 Answers

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

like image 129
Jeff Bowman Avatar answered Sep 25 '22 18:09

Jeff Bowman