'error: [Dagger/IncompatiblyScopedBindings] (unscoped) may not reference scoped bindings:
I don't understand how to resolve this error. I got this error after trying to add fragments to my application and using Dagger for DI. Here is the error stack:
error: [Dagger/IncompatiblyScopedBindings] di.component.ApplicationComponent (unscoped) may not reference scoped bindings: @Provides @di.ApplicationContext @di.ApplicationScope android.content.Context di.Module.ApplicationContextModule.getApplicationContext(application.MyApplication) @Provides @di.ApplicationScope android.content.SharedPreferences di.Module.SharedPreferencesModule.getSharedPreferences(@di.ApplicationContext android.content.Context) @Provides @di.ApplicationScope service.KeyStoreServiceInterface di.Module.KeyStoreModule.getKeyStoreService(@Named("KEY_STORE_FILE") java.io.File) @Provides @di.ApplicationScope repository.SharedPreferencesHelper di.Module.SharedPreferenceHelperModule.getSharedPreferencesHelper() @Provides @di.ApplicationScope service.CoinmarketcapService di.Module.CoinmarketcapModule.getCoinmarketcapService(com.google.gson.Gson, okhttp3.OkHttpClient) @Provides @di.ApplicationScope com.google.gson.Gson di.Module.GsonModule.getGson() @Provides @di.ApplicationScope okhttp3.OkHttpClient di.Module.OkHttpModule.getOkHttpClient() @Provides @di.ApplicationScope repository.WalletRepositoryInterface di.Module.WalletRepositoryModule.getWalletRepository(repository.SharedPreferencesHelper, service.KeyStoreService)
Here is my ApplicationComponent class:
@Component(modules = {ApplicationContextModule.class,
SharedPreferencesModule.class,
KeyStoreModule.class,
SharedPreferenceHelperModule.class,
AndroidInjectionModule.class,
BindModule.class,
AndroidSupportInjectionModule.class,
OkHttpModule.class,
GsonModule.class,
CoinmarketcapModule.class,
WalletRepositoryModule.class})
@SuppressWarnings("unchecked")
public interface ApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(MyApplication myApplication);
ApplicationComponent build();
}
void inject(MyApplication myApplication);
@ApplicationContext
Context getApplicationContext();
SharedPreferences getSharedPreferences();
KeyStoreServiceInterface getKeyStoreService();
SharedPreferencesHelper getSharedPreferencesHelper();
CoinmarketcapService getCoinmarketcapService();
WalletRepositoryInterface getWalletRepository();
}
I have a FragmentScope and an ActivityScope annotation in my android code. It's just regular scopes for Dagger with Retention.RUNTIME. Here is my application code:
public class MyApplication extends MultiDexApplication implements HasActivityInjector, HasFragmentInjector {
private ApplicationComponent applicationComponent;
@Inject
DispatchingAndroidInjector<Activity> dispatchingActivityInjector;
@Inject
DispatchingAndroidInjector<Fragment> fragmentDispatchingAndroidInjector;
@Override
public void onCreate() {
super.onCreate();
DaggerApplicationComponent
.builder()
.application(this)
.build()
.inject(this);
Timber.plant(new Timber.DebugTree());
}
@Override
public AndroidInjector<Activity> activityInjector() {
return dispatchingActivityInjector;
}
public ApplicationComponent getApplicationComponent() {
return applicationComponent;
}
@Override
public AndroidInjector<Fragment> fragmentInjector() {
return fragmentDispatchingAndroidInjector;
}
}
Any help would be appreciated.
EDIT: I managed to solve this by adding @ApplicationScope to my component. Why do I have to do this? Before adding fragments and @FragmentScope to my code (Before this I had only @activityscope and @applicationscope) I did not have this issue, it only appeared after adding fragments? If anyone can help answer this, it is still worth to accept that answer to help anyone else that might have this problem and wants to understand it.
Solution 1:[1]
I had the same issue, but after deleting the module providing Context from ApplicationComponent problem is gone.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kirkadev |
