1
votes

I'm building an Android app that is making extensive use of Dagger for dependency injection.

I've separated my app into multiple logical packages, and I tried to split things up into multiple modules that all get included in a "main" module, in order to separate concerns.

However, now I have a problem where I have to maintain the injects { ... } list for each module, and it is getting very tedious and error prone.

For instance

@Module(
        injects = {
                ThingA.class
        },
        complete = false,
        library = false
)
public class FeatureAModule {
    @Provides
    public FeatureA providesFeature(FeatureAImpl featureA){
        return featureA;
    }
}

@Module(
        injects = {
                ThingB.class,
                AnotherThingB.class
        },
        complete = false,
        library = false
)
public class FeatureBModule {
    @Provides
    public FeatureB providesFeature(FeatureBImpl feature){
        return feature;
    }
}

@Module(
        injects = {
        },
        complete = true,
        library = false,
        includes = { FeatureAModule.class, FeatureBModule.class }
)
public class ApplicationModule {

}

If I add an @Injects annotation in a class but forget to add it to my injects list, I'll get a runtime exception. If I have a class that uses features from 2 modules I'll typically add the injects to both Modules, but then if I forget one and then I go and remove it from one of the modules I have to make sure it's not used anywhere else or else I'll then have that runtime exception.

Basically, I am trying to figure out a foolproof way to never have that "no injects registered" when I try to inject something (ideally at the dagger-compiler level).

1

1 Answers

0
votes

i ran into the same issue...
my solution was to group the components of my app into the features they belong to

then i created a separate module for each feature which has all the classes which require injection in its injects list.

this way the entire thing became managable, yet the entire injects list should become obsolete