Skip to content
Advertisement

Dagger Component has conflicting scopes

I am using Dagger and I have an app component with @Singleton scope and also a subcomponent with @Singleton scope. Now when I compile it I get this error:

[io.droid.nowtellapp.dagger.SignInMvpComponet] io.droid.nowtellapp.dagger.SignInMvpComponet has conflicting scopes: io.droid.nowtellapp.dagger.ApplicationComponent also has @Singleton

To resolve this error I removed @Singleton from my subcomponet and compiled it, this time getting this error:

Error:(12, 1) error: io.droid.nowtellapp.dagger.SignInMvpComponet (unscoped) may not reference scoped bindings: @Singleton @Provides io.droid.nowtellapp.mvp.SignInMvp.Presenter io.droid.nowtellapp.dagger.SignInMvpModule.presenter(io.droid.nowtellapp.webservices.NowTellApi)

Also getting this error:

Error:(21, 8) error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.

Here is my ApplicationComponent

@Singleton
@Component(modules = {AppModule.class, RetroApiModule.class})
public interface ApplicationComponent {

void inject(MainActivity mainActivity);

SignInMvpComponet signInMvpComponet(SignInMvpModule signInMvpModule);
}

Here is my SignInMvpComponet

@Subcomponent(modules = {SignInMvpModule.class})
public interface SignInMvpComponet {
void inject(SignInFragment signInFragment);

And This is SignInMvpModule class

@Module
public class SignInMvpModule {
private final SignInMvp.View view;

public SignInMvpModule(SignInMvp.View view) {
    this.view = view;
}

@Singleton
@Provides
SignInMvp.Presenter presenter(NowTellApi api) {
    return new SignInPresenter(view,api);
}
}

How to resolve this problem? Thanks in advance.

Advertisement

Answer

I have an app component with @Singleton scope and also a subcomponent with @Singleton scope.

No you don’t. A subcomponent can not have the same scope as its parent. You can read in the documentation:

No subcomponent may be associated with the same scope as any ancestor component, although two subcomponents that are not mutually reachable can be associated with the same scope because there is no ambiguity about where to store the scoped objects.

Your first error is because parent and subcomponent share the same scope, your second error is because a subcomponent needs a scope. The solution to both errors is to give the subcomponent a different scope than the parent, or don’t use a subcomponent at all (if they both should be @Singleton, why do you need 2?)

Usually we tend to create scopes like @PerActivity, or @ActivityScoped for our subcomponents, which shares—as the name would indicate—the lifecycle of the Activity and gets used within it.

Advertisement