Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CustomScope may not reference bindings with different scopes

I am new to dagger, I have defined my application component like this

@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
    void inject(BaseActivity activity);
    Context context();
}

This is my ApplicationModule

@Module
public class ApplicationModule {

    public TipoApplication application;

    public ApplicationModule(TipoApplication application) {
        this.application = application;
    }

    @Singleton
    @Provides
    public Context provideContext(){return application.getApplicationContext();}

    @Singleton
    @Provides
    public SharedPreferences provideSharedPreferences(Context context){
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    @Singleton
    @Provides
    public Gson provideGson(){
        return new Gson();
    }

    @Singleton
    @Provides
    public SharedPrefsManager provideSharedPrefsManager(SharedPreferences sharedPreferences, Gson gson){
        return new SharedPrefsManager(sharedPreferences, gson);
    }
}

I have created a dependent Component LocationProviderComponent

@LocationScope
@Component(dependencies = {ApplicationComponent.class},modules = {LocationProviderModule.class})
public interface LocationProviderComponent {
    void inject(LocationRepository locationRepository);
}

And Finally My LocationProviderModule

@Module
public class LocationProviderModule {

    @Singleton
    @Provides
    FusedLocationProviderClient provideFusedLocationProviderClient(Context context) {
        return LocationServices.getFusedLocationProviderClient(context);

    }

    @Singleton
    @Provides
    LocationRequest provideLocationRequest() {
        return new LocationRequest()
                .setInterval(5000)
                .setFastestInterval(60000)
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    @Singleton
    @Provides
    LocationSettingsRequest provideLocationSettingRequest(LocationRequest mLocationRequest) {
        return new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest).build();
    }

}

I am getting 2 errors when I build.

1st cannot find symbol class DaggerApplicationComponent 2nd

LocationProviderComponent scoped with LocationScope may not reference bindings with different scopes:
@Singleton @Provides FusedLocationProviderClient LocationProviderModule.provideFusedLocationProviderClient(android.content.Context)
@Singleton @Provides LocationRequest .module.LocationProviderModule.provideLocationRequest()

Please tell me what I am doing wrong.

like image 677
jaydeep_gedia Avatar asked Dec 15 '18 11:12

jaydeep_gedia


1 Answers

Any module's @Provides method may only have the same scope as the component they are part of. Read more here.

In your case LocationProviderModule is part of the LocationProviderComponent which is scoped with @LocationScope whereas the provides methods in that module uses the @Singleton scope. This is exactly what Dagger is complaining about:

LocationProviderComponent scoped with LocationScope may not reference bindings with different scopes

It is also pointing to where the problem is:

@Singleton @Provides FusedLocationProviderClient LocationProviderModule.provideFusedLocationProviderClient(android.content.Context)
@Singleton @Provides LocationRequest.module.LocationProviderModule.provideLocationRequest()

Instead of using @Singleton, you just need to use @LocationScope in the LocationProviderModule.

like image 118
Abhishek Jain Avatar answered Nov 13 '22 05:11

Abhishek Jain