首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >匕首2,图书馆模块和@Singleton

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

Stack Overflow用户
提问于 2016-11-08 15:06:53
回答 2查看 4.9K关注 0票数 18

我试图在一个具有、多个Android模块的安卓项目中使用Dagger 2,我希望能够从这些模块中提供单例作用域实例。

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

我不能做的是提供一个单例的实例。

项目结构如下:

代码语言:javascript
复制
Project
├── app
├── library1
·
·
·
└── libraryN

在库中,我以这种方式定义组件:

代码语言:javascript
复制
@Component
public interface LibraryComponent {

    // Provide instances of MyManager to MainComponent:
    MyManager getMyManager();
}

MyManager看起来是这样的:

代码语言:javascript
复制
public class MyManager {

    private static final String TAG = "MyManager";

    @Inject
    public MyManager() {
        Log.d(TAG, "Creating MyManager");
    }
}

在主应用程序中,我以这样的方式定义我的组件:

代码语言:javascript
复制
@ApplicationScope
@Component(dependencies = {LibraryComponent.class, Library2Component.class})
public interface MainComponent {

    void inject(MainActivity target);
}

这是应用程序类:

代码语言:javascript
复制
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;
    }
}

如果我只向一个库组件添加一个作用域,那么我就可以作为一个单例提供管理器。但是,如果我试图对另一个库做同样的操作,我就会得到错误:

代码语言:javascript
复制
@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

同样,我想要实现的只是在我的主应用程序项目中注入由库项目提供的一些管理人员的单例作用域实例。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-09 13:21:58

正如@EpicPandaForce所建议的那样,使用Dagger模块而不是组件解决了我的问题

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

第一个是删除库组件并创建库模块:

代码语言:javascript
复制
@Module
public class LibraryModule {

    @Singleton
    @Provides
    MyManager provideMyManager(MyUtility myUtility) {
        return new MyManager(myUtility);
    }
}

而不只是在App组件中指定那些模块,而不是组件的依赖项:

代码语言:javascript
复制
@Singleton
@Component(modules = {LibraryModule.class, Library2Module.class})
public interface MainComponent {

    void inject(MainActivity target);
}

就是这样,这段代码用@Singleton作用域注释的Manager类只实例化了一次。

票数 13
EN

Stack Overflow用户

发布于 2016-11-09 09:00:30

尝试将库组件统一到一个组件下(例如: AllLibrariesComponent),然后让MainComponent只依赖于AllLibrariesComponent。

Library1:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
@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 *** ");
    }
}

应用程序:

代码语言:javascript
复制
@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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40490275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档