简体   繁体   English

Dagger不会为Android生成Dagger类

[英]Dagger won't generate Dagger classes for Android

I'm trying to set up a project with Dagger. 我正在尝试与Dagger建立一个项目。 Right now when I build, none of the Dagger* classes are generated for the components, and I'm getting the following errors: 现在,当我构建时,没有为组件生成任何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)

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

With a parent class RedditAppGraph: 使用父类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: 包含引发错误的提供程序方法的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();
            }
        };
    }
}

I've tried rebuilding the project to see if it will generate the classes. 我试过重建项目,看它是否会生成类。

It looks like ActivityScreenSwitcherServer is a qualifier. 看来ActivityScreenSwitcherServer是一个限定词。 If that's the case, you're binding your ActivityHierarchyServer with the qualifier, but exposing ActivityHierarchyServer on your component (through RedditAppGraph ) without it. 在这种情况下,您要将ActivityHierarchyServer与限定符绑定在一起,但不使用它就将ActivityHierarchyServer暴露在组件上(通过RedditAppGraph )。 Either remove the qualifier from your @Provides method or add it to the component interface. @Provides方法中删除限定符或将其添加到组件接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM