CustomScope may not reference bindings with different scopes

帅比萌擦擦* 提交于 2020-06-23 05:56:05

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/53791779/customscope-may-not-reference-bindings-with-different-scopes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!