I'm trying to set up a project with Dagger. Right now when I build, none of the Dagger* classes are generated for the components, and I'm getting the following errors:
Error:(10, 8) error: [com.redditapp.dagger.RedditAppGraph.inject(com.redditapp.RedditApplication)]
com.redditapp.ui.ActivityHierarchyServer cannot be provided without an @Provides- or @Produces-annotated method.
com.redditapp.ui.ActivityHierarchyServer is injected at
com.redditapp.RedditApplication.activityHierarchyServer
com.redditapp.RedditApplication is injected at
com.redditapp.dagger.RedditAppGraph.inject(app)
and
Error:(13, 10) error: com.redditapp.base.navigation.activity.ActivityScreenSwitcher cannot be provided without an @Inject constructor or from an @Provides-annotated method.
com.redditapp.base.navigation.activity.ActivityScreenSwitcher is injected at
com.redditapp.ui.screens.home.HomePresenter.<init>(screenSwitcher)
com.redditapp.ui.screens.home.HomePresenter is injected at
com.redditapp.ui.screens.home.HomeActivity.presenter
com.redditapp.ui.screens.home.HomeActivity is injected at
com.redditapp.ui.screens.home.HomeComponent.inject(activity)
Here's my current setup.
Application-level class uses:
public void buildComponentAndInject() {
component = RedditAppComponent.Initializer.init(this);
component.inject(this);
}
The RedditAppComponent looks like this and DaggerRedditAppComponent isn't generated and therefore is red:
@ApplicationScope
@Component(modules = { RedditAppModule.class, UiModule.class})
public interface RedditAppComponent extends RedditAppGraph {
/**
* An initializer that creates the graph from an application.
*/
final class Initializer {
public static RedditAppComponent init(RedditApplication app) {
return DaggerRedditAppComponent.builder()
.redditAppModule(new RedditAppModule(app))
.uiModule(new UiModule())
.build();
}
private Initializer() {} // No instances.
}
}
With a parent class RedditAppGraph:
public interface RedditAppGraph {
void inject(RedditApplication app);
ViewContainer viewContainer();
ActivityHierarchyServer activityHierarchyServer();
}
The UI module that contains the provider methods that are throwing the errors looks like:
@Module
public class UiModule {
@Provides
@ApplicationScope
ActivityScreenSwitcher provideActivityScreenSwitcher() {
return new ActivityScreenSwitcher();
}
@Provides
@ApplicationScope
@ActivityScreenSwitcherServer
ActivityHierarchyServer provideActivityHierarchyServer(final ActivityScreenSwitcher screenSwitcher) {
return new ActivityHierarchyServer.Empty() {
@Override
public void onActivityStarted(Activity activity) {
screenSwitcher.attach(activity);
}
@Override
public void onActivityStopped(Activity activity) {
screenSwitcher.detach();
}
};
}
}
I've tried rebuilding the project to see if it will generate the classes.
It looks like
ActivityScreenSwitcherServer
is a qualifier. If that's the case, you're binding yourActivityHierarchyServer
with the qualifier, but exposingActivityHierarchyServer
on your component (throughRedditAppGraph
) without it. Either remove the qualifier from your@Provides
method or add it to the component interface.