Previously, I had only one AppComponent with four modules (AppModule, NetworkModule, StorageModule, PresentersModule) and everywhere introduced singleton. Recently, I decided to do a little refactoring in my application and divide it into areas. I think leaders can only live within the framework of actions, so I created @ActivityScope and ActivityModule, but the project cannot be compiled due to my lack of understanding how to mix these areas. I have read many articles and questions on stackoverflow, but everywhere there are simple examples where the modules are independent. In my case, a thing like
@Singleton @Component(modules = { AppModule.class, StorageModule.class, NetworkModule.class }) public interface AppComponent { ActivityComponent plus(PresentersModule module);
does not work. I get this error:
Error:(19, 1) error: com.my.package.di.component.ActivityComponent scoped with @com.my.package.di.scope.ActivityScope may not reference bindings with different scopes: @Provides @Singleton android.app.Application com.my.package.di.module.AppModule.provideApplication() @Provides @Singleton com.my.package.network.FeedBurnerApi com.my.package.di.module.NetworkModule.provideFeedBurnerApi(android.app.Application) @Provides @Singleton android.database.sqlite.SQLiteOpenHelper com.my.package.di.module.StorageModule.provideSQLiteOpenHelper(android.app.Application) @Provides @Singleton com.my.package.storage.Repository com.my.package.di.module.StorageModule.provideRepository(android.database.sqlite.SQLiteOpenHelper) @Provides @Singleton com.my.package.SharedPreferencesHelper com.my.package.di.module.StorageModule.provideSharedPreferencesHelper(android.app.Application)
So the question is, how can I get an instance of my ActivityComponent?
You can see the dependencies between the modules below:
Application module:
@Module public final class AppModule { private final MyApplication mApplication; public AppModule(MyApplication application) { ... } @Provides @Singleton Application provideApplication() { ... } }
Network module:
@Module(includes = { AppModule.class }) public final class NetworkModule { @Provides @Singleton FeedBurnerApi provideFeedBurnerApi(Application application) { ... } @Provides @Singleton Retrofit provideRetrofit() { ... } }
Storage module:
@Module(includes = { AppModule.class }) public final class StorageModule { @Provides @Singleton Repository provideRepository(SQLiteOpenHelper sqLiteOpenHelper) { ... } @Provides @Singleton SQLiteOpenHelper provideSQLiteOpenHelper(Application application) { ... } @Provides @Singleton SharedPreferencesHelper provideSharedPreferencesHelper(Application application) { ... } }
Speakers module:
@Module(includes = { AppModule.class, NetworkModule.class, StorageModule.class }) public final class PresentersModule { @Provides FeedPageViewPresenter provideFeedPageViewPresenter(FeedBurnerApi api, Repository repository, SharedPreferencesHelper preferences) { ... } @Provides @ActivityScope SlidingTabsViewPresenter provideSlidingTabsViewPresenter(Repository repository) { ... } }
Application Component:
@Singleton @Component(modules = { AppModule.class, StorageModule.class, NetworkModule.class }) public interface AppComponent {}
Event:
@Subcomponent(modules = PresentersModule.class) @ActivityScope public interface ActivityComponent { void inject(FeedPageView view); void inject(SlidingTabsView view); }