私はアンドロイドでDagger2
を使用する初心者です。私はcontext
を使用しているDaggerモジュールとしていくつかのクラスを作成します。それらを必要とする他のモジュールにマージ、結合、または単一のコンテキストを使用することはできません。そして私は今、このエラーが出る:Android複数のDagger2モジュールに単一のコンテキストを使用
android.content.Context is bound multiple times
SpModules:
@Module
public class SpModules {
private Context context;
public SpModules(Context context) {
this.context = context;
}
@Provides // this can be non-scoped because anyway the same instance is always returned
Context provideContext() {
return this.context;
}
@Provides
@Singleton
SP provideSharePreferences(Context context) {
return new SP(context); // use method-local Context
}
}
RealmModule
:
@Module
public class RealmModule {
private Context context;
@Provides
Context provideApplicationContext() {
return AlachiqApplication.getInstance();
}
@Provides
@Singleton
RealmConfiguration provideRealmConfiguration() {
final RealmConfiguration.Builder builder = new RealmConfiguration.Builder()
.schemaVersion(Migration.SCHEMA_VERSION)
.deleteRealmIfMigrationNeeded()
.migration(new Migration());
return builder.build();
}
@Provides
Realm provideDefaultRealm(RealmConfiguration config) {
return Realm.getInstance(config);
}
@Provides
Context provideContext() {
return this.context;
}
}
コンポーネント:
@Component(modules = {RealmModule.class, SpModules.class})
@Singleton
public interface ApplicationComponent {
void inject(ActivityRegister target);
void inject(ActivityMain target);
void inject(ActivityBase target);
void inject(FragmentAlachiqChannels target);
void inject(SocketServiceProvider target);
}
、その後、Applicationクラスを作るためにDagger2:
component = DaggerApplicationComponent.builder()
.appModules(new SpModules(this))
.build();
どうすればこの問題を解決できますか?