Dagger不会为Android生成Dagger类

时间:2016-12-19 03:11:12

标签: java android dagger-2 dagger

我正试图与Dagger建立一个项目。现在,当我构建时,没有为组件生成任何Dagger *类,并且我得到以下错误:

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)

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)

这是我目前的设置。

应用程序级别使用:

public void buildComponentAndInject() {
        component = RedditAppComponent.Initializer.init(this);
        component.inject(this);
    }

RedditAppComponent看起来像这样,DaggerRedditAppComponent没有生成,因此是红色的:

@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.
    }
}

使用父类RedditAppGraph:

public interface RedditAppGraph {
    void inject(RedditApplication app);
    ViewContainer viewContainer();
    ActivityHierarchyServer activityHierarchyServer();
}

包含抛出错误的提供程序方法的UI模块如下所示:

@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();
            }
        };
    }
}

我尝试重建项目以确定它是否会生成类。

1 个答案:

答案 0 :(得分:2)

看起来ActivityScreenSwitcherServer是限定词。如果是这种情况,您将ActivityHierarchyServer与限定符绑定,但在没有它的情况下在您的组件上展示ActivityHierarchyServer(通过RedditAppGraph)。从@Provides方法中删除限定符或将其添加到组件界面。