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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With