以前,我只有一个带有四个模块(AppModule,NetworkModule,StorageModule,PresentersModule)的AppComponent,并在各处注入(inject)了单例。最近,我决定在我的应用程序中进行小的重构,并将其划分为多个作用域。我认为,演示者只能生活在 Activity 中,因此我创建了@ActivityScope和ActivityModule,但是由于我对如何混合使用这些范围的误解,无法编译该项目。
我在stackoverflow上阅读了很多文章和问题,但是到处都有简单的示例,其中模块是独立的。就我而言
@Singleton
@Component(modules = { AppModule.class, StorageModule.class, NetworkModule.class })
public interface AppComponent {
ActivityComponent plus(PresentersModule module); // <-- 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)
因此,问题是如何获取我的ActivityComponent实例?
您可以在下面查看模块之间的依赖关系:
应用模块:
@Module
public final class AppModule {
private final MyApplication mApplication;
public AppModule(MyApplication application) { ... }
@Provides @Singleton Application provideApplication() { ... }
}
网络模块:
@Module(includes = { AppModule.class })
public final class NetworkModule {
@Provides @Singleton FeedBurnerApi provideFeedBurnerApi(Application application) { ... }
@Provides @Singleton Retrofit provideRetrofit() { ... }
}
存储模块:
@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) { ... }
}
演示者模块:
@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) { ... }
}
应用组件:
@Singleton
@Component(modules = { AppModule.class, StorageModule.class, NetworkModule.class })
public interface AppComponent {}
Activity 组成部分:
@Subcomponent(modules = PresentersModule.class)
@ActivityScope
public interface ActivityComponent {
void inject(FeedPageView view);
void inject(SlidingTabsView view);
}
最佳答案
问题出在我的PresentersModule中。
由于子组件可以从其父级访问整个对象图,因此我不需要在我的子模块中包括这些依赖项。
因此,我更改了此代码:
@Module(includes = { AppModule.class, NetworkModule.class, StorageModule.class })
public final class PresentersModule { ... }
有了这个:
@Module
public final class PresentersModule { ... }
它解决了我的问题。
关于android - Dagger 2混合镜,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33620909/