Dagger 不会为 Android 生成 Dagger 类

Dagger won't generate Dagger classes for Android

我正在尝试使用 Dagger 建立一个项目。现在,当我构建时,为组件生成 none of the Dagger* classes,我收到以下错误:

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)

这是我当前的设置。

Application-level class 使用:

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

有了 parent class 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();
            }
        };
    }
}

我已经尝试重建项目以查看它是否会生成 classes。

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