简体   繁体   English

如何在android库项目中使用dagger

[英]How to use dagger in a android library project

I'm currently trying to add Dagger to my android projects. 我正在尝试将Dagger添加到我的android项目中。 For the apps projects its easy and clear to me, how to build the ObjectGraph. 对于应用程序项目,我很容易清楚,如何构建ObjectGraph。 But I dont quite know whats the best way to do this in my android library projects. 但我不知道在我的android库项目中最好的方法是什么。

Should I keep building the ObjectGraph in the Application class of the apps and pass the OG over to a LibraryModule - plussing the OG of library to the Apps OG? 我应该继续在应用程序的Application类中构建ObjectGraph并将OG传递给LibraryModule - 将库的OG插入到Apps OG中吗? Or should i build the whole ObjectGraph in the library? 或者我应该在库中构建整个ObjectGraph?

What if I need to inject a class in the library by ObjectGraph.inject(this) ? 如果我需要通过ObjectGraph.inject(this)在库中注入一个类怎么办? In my Apps projects I can get the OG from the Application class. 在我的Apps项目中,我可以从Application类中获取OG。 But how to handle this in the library? 但是如何在图书馆处理这个? Should I add a @Provides method for the ObjectGraph? 我应该为ObjectGraph添加@Provides方法吗?

Big thanks for your help. 非常感谢你的帮助。

Edit: In short: How can I call ObjectGraph.inject(this) in my library project where I don't have access to the OG because it is being builded in the Application Class? 编辑:简而言之:如何在我的库项目中调用ObjectGraph.inject(this) ,因为它是在Application类中构建的?

In case someone using Dagger 2 gets here, this is the way I've done in my App: 如果有人使用Dagger 2来到这里,这就是我在我的应用程序中所做的方式:

In the library module I've created the following Module and Component: 在库模块中,我创建了以下模块和组件:

@Module
public class ModuleUtil {

    @Provides
    public RestTemplate provideRestTemplate() {
        return new RestTemplate();
    }

}

@Singleton
@Component(
        modules = {
                ModuleUtil.class
        })
public interface MainComponent {
    void inject(Postman postman);
}

And then I've created the Singleton below in order to manage the injections: 然后我在下面创建了Singleton以管理注射:

public class DaggerWrapper {

    private static MainComponent mComponent;

    public static MainComponent getComponent() {
        if (mComponent == null) {
            initComponent();
        }
        return mComponent;
    }

    private static void initComponent () {
       mComponent = DaggerMainComponent
                .builder()
                .utilModule(new ModuleUtil())
                .build();
    }
}

When some class from the library module needs to inject its members, I simply call DaggerWrapper.getComponent().inject(this); 当库模块中的某个类需要注入其成员时,我只需调用DaggerWrapper.getComponent().inject(this); and that't it. 那就是它。

I'm doing this way: 我是这样做的:

  1. @Module classes belong to the main project and they provide implementations which you are injecting to library elements, so there are no @Module classes in the library projects @Module类属于主项目,它们提供了向库元素注入的实现,因此库项目中没有@Module类

  2. Library elements which are expecting dependency must have access to ObjectGraph and call .inject() on themselves, but main project should give ObjectGraph instance to the library with provided @Module dependency 期望依赖的库元素必须能够访问ObjectGraph并自己调用.inject(),但主项目应该使用提供的@Module依赖项将ObjectGraph实例赋予库

  3. How to get ObjectGraph from main project into the library? 如何将ObjectGraph从主项目导入库中? You could have interface like this: 你可以有这样的界面:

     interface Injector { void inject(Object object); public ObjectGraph getObjectGraph(); } 

Context objects like Activity or Application class implements this interface (holders of ObjectGraph objects). 像Activity或Application类这样的Context对象实现了这个接口(ObjectGraph对象的持有者)。

If you have example of Activity in the library module which needs something to inject from the main project this would look like this: 如果你在库模块中有Activity的例子,它需要从主项目中注入一些东西,它将如下所示:

class LibraryActivity extends Activity {

    @Inject ActivationModule instance;

    void onCreate(... ) {
        Injector injector = (Injector)getApplicationContext();
        injector.inject(this)
    }
}

ActivationModule is the class/interface in the library project. ActivationModule是库项目中的类/接口。

Main project has application class which implements Injector interface and creates ObjectGraph with provided dependecy for ActivationModule in the library project. 主项目具有应用程序类,该类实现Injector接口并创建ObjectGraph,并为库项目中的ActivationModule提供依赖性。

class MyApplicationInTheMainProject extends Application implements Injector {

    ObjectGraph graph;

    @Override
    public void onCreate() {
        super.onCreate();
        graph = ObjectGraph.create(new ActivationModuleImpl(this));
    }

    @Override public void inject(Object object) {
        graph.inject(object);
    }

    @Override public ObjectGraph getObjectGraph() {
        return graph;
    }
}


@Module(injects = {
        LibraryActivity.class
}, library = true)
class ActivationModuleImpl implements ActivationModule {
    ....
}

if you are giving this library to people and they dont know nothing about your scenario so you must write it in a way that your Dagger works perfectly without any help from user. 如果您将这个图书馆提供给人们并且他们对您的场景一无所知,那么您必须以您的Dagger完美工作的方式编写它,而无需用户的任何帮助。 (the easier to work with the better practice) (使用更好的练习更容易)

i just wrote some library for you to show how to do it. 我刚刚为你写了一些图书馆来展示如何做到这一点。 i wrote the library in a way that you can even run it standalone and see the result in the messages tab. 我以一种你甚至可以独立运行它的方式编写库,并在消息选项卡中查看结果。 user of your library doesnt need to know nothing about dagger and does nothing he just uses the library and dagger will be configured: 你的图书馆的用户不需要对匕首一无所知,也不做任何他只是使用图书馆和匕首的配置:

https://github.com/amirziaratii/libraryUsingDagger.git https://github.com/amirziaratii/libraryUsingDagger.git

if this library is something you use it yourself and for your own project, the best practice is do it like in this project of my friend: 如果这个库是您自己使用的,对于您自己的项目,最好的做法就是在我的朋友的这个项目中这样做:

https://github.com/mmirhoseini/trakt.tv https://github.com/mmirhoseini/trakt.tv

all your questions are answered in these two projects. 您在这两个项目中回答了所有问题。 ask any question and ill answer in comment. 在评论中提出任何疑问和错误答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM