错误:[Dagger / DuplicateBindings] com.example.StartRouter已绑定多次? [英] error: [Dagger/DuplicateBindings] com.example.StartRouter is bound multiple times?

查看:179
本文介绍了错误:[Dagger / DuplicateBindings] com.example.StartRouter已绑定多次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个课程 StartRouter 。我想为每个活动提供此类的不同实例,并在活动仍然存在的情况下保持活动状态(为此,我创建了 @ActivityScope )。

I have a class StartRouter. I want to provide different instances of this class to each activity, and keep it alive as long as activity is alive (for that I made @ActivityScope).

问题是它在我的代码中没有做到这一点。在我的情况下,匕首似乎为每个注释提供了一个实例,对于 @ActivityScope 而言,它一次。此范围是自定义匕首范围。

Problem is that it does not do that in my code. It seems as dagger provides one instance per annotation, in my case, once for the @ActivityScope. This scope is a custom dagger scope.

这是我的两个模块(每个活动一个),第一个模块:

Here are my two modules (One for each activity), first module:

@Module
public class SplashModule {

    @Provides
    SplashVMFactory splashVMFactory(StartRouter startRouter){
        return new SplashVMFactory(startRouter);


    @Provides
    StartRouter startRouter(){
        return new StartRouter();
    }

}

第二个模块:

@Module
public class QuestionModule {

    @Provides
    QuestionVMFactory questionVMFactory(StartRouter startRouter){
        return new QuestionVMFactory(startRouter);
    }

    @Provides
    StartRouter startRouter(){
        return new StartRouter();
    }

}

我有这个:

@Module
public abstract class BinderModule {

    @ActivityScope
    @ContributesAndroidInjector(modules = SplashModule.class)
    abstract SplashActivity bindSplashActivity();

    @ActivityScope
    @ContributesAndroidInjector(modules = QuestionModule.class)
    abstract QuestionActivity bindQuestionActivity();

}

我的应用程序组件:

@ApplicationScope
@Component(modules = {AndroidInjectionModule.class,
        BinderModule.class,
        SplashModule.class,
        QuestionModule.class})
public interface AppComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(App app);
        AppComponent build();
    }

    void inject(App app);

}

当我运行此命令时,出现此错误:

When I run this I get this error:

error: [Dagger/DuplicateBindings] com.example.StartRouter is bound multiple times:
@Provides com.example.StartRouter com.example.QuestionModule.startRouter()
@Provides com.example.StartRouter com.example.SplashModule.startRouter()
com.example.StartRouter is injected at
com.example.LoginModule.loginVMFactory(…, startRouter)
com.example.LoginVMFactory is injected at
com.example.LoginActivity.factory
com.example.LoginActivity is injected at
dagger.android.AndroidInjector.inject(T) [com.example.AppComponent ? com.example.BinderModule_BindLoginActivity.LoginActivitySubcomponent]
It is also requested at:
com.example.QuestionModule.questionVMFactory(…, startRouter)
The following other entry points also depend on it:
dagger.android.AndroidInjector.inject(T) [com.example.AppComponent ? com.example.BinderModule_BindQuestionActivity.QuestionActivitySubcomponent]

(除了两个示例,其他模块还提供了StartRouter作为您可以从我的错误中看到)。

(Also have StartRouter provided in other modules than the two examples as you can see from my error).

我想,如果我理解正确,那是由于使用 @ActivityScope BinderModule @ContributesAndroidInjector 中的c>会为每个活动的活动创建不同的组件,只要活动本身即可,但是我似乎理解错了?

I thought, if I have understood right, that from the usage of @ActivityScope in BinderModule with @ContributesAndroidInjector would create different Components for each activity that is alive as long as the activity itself, but I seem to have understood wrong?

如果我在 StartRouter > @module ,方法是使用 @named 并提供1个模块,而无需提供 @named 然后通过提供不同的 StartRouter 实例来解决问题?但这对我的问题没有帮助。简而言之,我的问题是我无法提供有效的 @ActivityScoped StartRouter 实例(每个活动1个)与活动本身一样充实。关于如何执行此操作的任何建议?

If I differentiate between the different StartRouter in my @modules by using @named and having 1 module to provide without @named then that will solve the problem by providing different StartRouter instances? But that does not help me with my problem. My problem is, in short, I have trouble providing @ActivityScoped StartRouter instances (1 for each activity) that is alive as loong as the activity itself. Any suggestions on how I could do that?

推荐答案

您的设置很好,但是您也 将两个模块(都绑定 StartRouter )都添加到 AppComponent ,因此多次绑定该错误。 / p>

Your setup is fine but for the fact that you also add both modules (both binding StartRouter) to AppComponent, thus the error that you bind it multiple times.

@Component(modules = {AndroidInjectionModule.class,
        BinderModule.class,
        SplashModule.class,    // <== 1.
        QuestionModule.class}) // <== 2.
public interface AppComponent { .. }

由于您希望每个Activity一个实例,因此无需将模块添加到 AppComponent 。您应该可以从其中删除这两个模块。

Since you want one instance per Activity there is no need to add the modules to AppComponent. You should be able to remove those 2 modules from it.

这篇关于错误:[Dagger / DuplicateBindings] com.example.StartRouter已绑定多次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆