我试图在一个具有、多个Android模块的安卓项目中使用Dagger 2,我希望能够从这些模块中提供单例作用域实例。
目前,我能够在库模块中定义组件,并在主应用程序模块中注入实例。
我不能做的是提供一个单例的实例。
项目结构如下:
Project
├── app
├── library1
·
·
·
└── libraryN在库中,我以这种方式定义组件:
@Component
public interface LibraryComponent {
// Provide instances of MyManager to MainComponent:
MyManager getMyManager();
}MyManager看起来是这样的:
public class MyManager {
private static final String TAG = "MyManager";
@Inject
public MyManager() {
Log.d(TAG, "Creating MyManager");
}
}在主应用程序中,我以这样的方式定义我的组件:
@ApplicationScope
@Component(dependencies = {LibraryComponent.class, Library2Component.class})
public interface MainComponent {
void inject(MainActivity target);
}这是应用程序类:
public class App extends Application {
private MainComponent component;
@Override
public void onCreate() {
super.onCreate();
component = DaggerMainComponent.builder()
.libraryComponent(DaggerLibraryComponent.create())
.library2Component(DaggerLibrary2Component.create())
.build();
}
public MainComponent getComponent() {
return component;
}
}如果我只向一个库组件添加一个作用域,那么我就可以作为一个单例提供管理器。但是,如果我试图对另一个库做同样的操作,我就会得到错误:
@com.codeblast.dagger2lib.ApplicationScope com.codeblast.dagger2lib.MainComponent depends on more than one scoped component:
@Component(dependencies = {LibraryComponent.class, Library2Component.class})
^
@com.codeblast.library.LibraryScope com.codeblast.library.LibraryComponent
@com.codeblast.library2.Library2Scope com.codeblast.library2.Library2Component同样,我想要实现的只是在我的主应用程序项目中注入由库项目提供的一些管理人员的单例作用域实例。
发布于 2016-11-09 13:21:58
正如@EpicPandaForce所建议的那样,使用Dagger模块而不是组件解决了我的问题。
接下来,我必须要做的必要的改变。
第一个是删除库组件并创建库模块:
@Module
public class LibraryModule {
@Singleton
@Provides
MyManager provideMyManager(MyUtility myUtility) {
return new MyManager(myUtility);
}
}而不只是在App组件中指定那些模块,而不是组件的依赖项:
@Singleton
@Component(modules = {LibraryModule.class, Library2Module.class})
public interface MainComponent {
void inject(MainActivity target);
}就是这样,这段代码用@Singleton作用域注释的Manager类只实例化了一次。
发布于 2016-11-09 09:00:30
尝试将库组件统一到一个组件下(例如: AllLibrariesComponent),然后让MainComponent只依赖于AllLibrariesComponent。
Library1:
@Component
@Singleton
public interface LibraryComponent {
// Provide instances of MyManager to MainComponent:
MyManager getMyManager();
}
@Singleton
public class MyManager {
private static final String TAG = "MyManager";
@Inject
public MyManager() {
Log.d(TAG, "*** Creating MyManager 1 ***");
}
}Library2:
@Singleton
@Component
public interface Library2Component {
// Provide instances of MyManager to MainComponent:
MyManager2 getManager2();
}
@Singleton
public class MyManager2 {
private static final String TAG = "MyManager";
@Inject
public MyManager2() {
Log.d(TAG, "*** Creating MyManager 2 *** ");
}
}应用程序:
@Singleton
@Component
public interface AllLibrariesComponent extends Library2Component, LibraryComponent{
}
@PerApplication
@Component(dependencies = AllLibrariesComponent.class)
public interface MainComponent {
void inject(MainActivity activity);
}
// ....
// and the instantication in your application class:
mainComponent = DaggerMainComponent.builder()
.allLibrariesComponent(DaggerAllLibrariesComponent.create())
.build();https://stackoverflow.com/questions/40490275
复制相似问题