1

I'm using dagger:1.2.2. There're two modules. BaseModule serves an EventBus for an Activity and ActivityModule serves NavigationController for the same Activity.

@Module(injects = { `does NOT have MainActivity.class registered`},
     library = true
)
public class BaseModule {
    @Provides @Singleton public EventBus provideEventBus() {
        return DefaultEventBus.create();
    }

    @Provides @Singleton public Application provideApplication() {
        return mApp;
    }

    @Provides @IsTablet public boolean isTablet(Application app) {
        return app.getResources().getBoolean(R.bool.is_tablet);
    }
}

@Module(
     addsTo = BaseModule.class,
     injects = MainActivity.class
)
public class ActivityModule {
    @Provides
    NavigationController provideNavigationController(Application app,@IsTablet boolean isTablet) {
         return new NavigationController(app, isTablet);
    }
}

Activity's fields:

@Inject
EventBus mEventBus;

@Inject
NavigationController mNavigationController;

NavigationController's ugly ctor:

public NavigationController(Context context, FragmentManager fm, List<Behaviour> behaviourList, @Nullable Class lastRoot,
                                @IsTablet boolean isTablet, Application application) {

This code passes static validation and performs well with

protected void onCreate(Bundle savedInstanceState) {
    ObjectGraph activityGraph = getBaseModuleGraph(this).plus(new ActivityModule(this));
    activityGraph.inject(this);
}

It crushes as expected due to unsatisfied dependency provided by ActivityModule if i just apply:

getBaseModuleGraph(this).inject(this);

The thing that bugs me is that BaseModule's inject doesn't contain MainActivity while it provides EventBus dependency for it. If I add this inject it gonna complain about unresolved NavigationController that is provided by ActivityModule. Setting complete=false on the BaseModule results in runtime exception

NavigationController has no injectable members. Do you want to add an injectable constructor? required by class MainActivity

I don't quite get it.

Ultimately I wanted BaseModule's injects to be more explicit by including MainActivity because it provides EventBus as I mentioned.

Could you help me out understanding it?

midnight
  • 3,420
  • 3
  • 36
  • 58
  • you should totally show the missing `{...}` in your code, for the base and activity modules. At least for the classes that you have trouble with (`NavigationController`) – EpicPandaForce Apr 14 '15 at 08:26
  • That is wonderful, but I can't see the problem without seeing the `@Provides` methods (or injectable constructor), and where you want to use this in your activity (`@Inject Whatever whatever` and `public void onCreate(...) { super.onCreate(...); getBaseModuleGraph(this).inject(this);`) so it would be nice if those were added to the question. – EpicPandaForce Apr 14 '15 at 08:39
  • where do you get `(Application app,@IsTablet boolean isTablet)` the two parameters from? The object graph doesn't seem to know about them. Although to be honest, you are not passing them to the navigation controller either, so I don't see their purpose. – EpicPandaForce Apr 14 '15 at 08:45
  • So, what do u have to say now? – midnight Apr 14 '15 at 08:54
  • add `(complete=false, library=true)` to ActivityModule and see if it fixes the problem - if not, just remove the two parameters from `provideNavigationController(Application app,@IsTablet boolean isTablet)` as you don't seem to be using them. But if you **are** using them, then change the constructor of `NavigationController` to receive the two parameters you get in the `provides` method. – EpicPandaForce Apr 14 '15 at 08:56
  • Changed NavigationController and added (complete=false, library=true). The same issue: NavigationController has no injectable members. Do you want to add an injectable constructor? required by class MainActivity – midnight Apr 14 '15 at 09:03
  • can you show how you the `NavigationController` class is initialized? – EpicPandaForce Apr 14 '15 at 09:07
  • @EpicPandaForce, added to the description. I feel like its not going anywhere, but anyway great thanks for the effort. I might try the problem on a separate project – midnight Apr 14 '15 at 09:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75181/discussion-between-midnight-and-epicpandaforce). – midnight Apr 14 '15 at 09:12
  • Hmm. I'm stumped. Especially with that giga-constructor, haha. Not sure how the other constructor compiles if this is the one you use, but okay. Anyways, what I used for Dagger was this setup: http://stackoverflow.com/questions/27036933/how-to-set-up-dagger-dependency-injection-from-scratch-in-android-project I'm not sure if it'll help, though. – EpicPandaForce Apr 14 '15 at 09:15
  • 1
    Look into http://antonioleiva.com/dagger-3/ and see what he does differently – EpicPandaForce Apr 14 '15 at 09:44

0 Answers0