匕首2,图书馆模块和@Singleton

我试图在一个Android项目中使用Dagger 2,该项目有几个Android库模块 ,我希望能够从这些模块中提供单例范围的类实例

目前,我可以在库模块中定义组件,并在主应用程序模块中注入实例。

我不能做的是提供一个实例作为单身人士。

项目结构如下:

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 

再次,我想要实现的只是注入我的主要应用程序项目由库项目提供的一些pipe理器的单例范围的实例

Solutions Collecting From Web of "匕首2,图书馆模块和@Singleton"

正如@EpicPandaForcebuild议的那样, 使用Dagger Modules而不是Components来解决我的问题

接下来,我必须做出必要的改变。

第一个是删除库组件和创build库模块:

 @Module public class LibraryModule { @Singleton @Provides MyManager provideMyManager(MyUtility myUtility) { return new MyManager(myUtility); } } 

不仅仅是在应用程序组件中指定这些模块,而是代替组件的依赖关系:

 @Singleton @Component(modules = {LibraryModule.class, Library2Module.class}) public interface MainComponent { void inject(MainActivity target); } 

就是这样,使用@Singleton作用域注释的Manager类的代码只能正确地实例化一次。

尝试将单个组件下的库组件统一(例如: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();