Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 865
Zeeshan Shabbir Avatar asked Jan 27 '18 07:01

Zeeshan Shabbir


People also ask

What does dagger component do?

Dagger components. Dagger can create a graph of the dependencies in your project that it can use to find out where it should get those dependencies when they are needed. To make Dagger do this, you need to create an interface and annotate it with @Component .

What is sub component in dagger?

Dagger 2 for Dummies in Kotlin — Subcomponent In Dagger 2, component is the main container-like object that binds all the dependencies (or it's factory). Subcomponent are components that is like an extension to its parent component. It can be used for. Partition the dependencies into different compartments.

What is a dagger module?

Modules are a way of telling Dagger how to provide dependencies from the dependency graph. These are typically high level dependencies that you aren't already contributing to the dependency graph through the @Inject constructor annotation we discussed in our previous article.

What is dagger programming?

Dagger is a fully static, compile-time dependency injection framework for Java, Kotlin, and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.


1 Answers

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.

like image 171
David Medenjak Avatar answered Oct 22 '22 20:10

David Medenjak