Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton component cannot depend on scoped components

While working on Android application using Dagger2 for dependency injects while defining Dagger component I'm getting this error

Error:(13, 1) error: This @Singleton component cannot depend on scoped components:
@Singleton com.eaxample.app.DaggerAndroid.networkhandler.WebserviceComponent

My code of component is here:

@Singleton
@Component(modules = {WebserviceModule.class}, dependencies = {ApplicationComponent.class})
public interface WebserviceComponent {
      WebserviceHelper providesWebserviceHelper();
}

Code of componeent in which I'm getting error is:

@Singleton
@Component(modules = {RemoteDataModule.class}, dependencies = {WebserviceComponent.class})
public interface RemoteDataSourceComponent {
       RemoteDataSource providesRemoteDataSource();
}

Why am I getting this error and how to resolve this?

like image 376
Sushant Kumar Avatar asked Feb 22 '17 09:02

Sushant Kumar


1 Answers

While dmitriyzaitsev's answer explains why you get the error, here's how you can solve it:

  1. Define your own scope (it will actually also behave like a Singleton scope). E.g. a file called RemoteDataScope.java:

    @Scope
    @Retention(RetentionPolicy.RUNTIME)
    public @interface RemoteDataScope {
    }
    
  2. Use the new scope, e.g. @RemoteDataScope instead of @Singleton in your RemoteDataSourceComponent:

    @RemoteDataScope
    @Component(modules = {RemoteDataModule.class}, dependencies = {WebserviceComponent.class})
    public interface RemoteDataSourceComponent {
           RemoteDataSource providesRemoteDataSource();
    }
    
like image 143
RumburaK Avatar answered Sep 20 '22 12:09

RumburaK