java 如何在android库项目中使用dagger
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26833389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use dagger in a android library project
提问by sebastian
I'm currently trying to add Dagger to my android projects. For the apps projects its easy and clear to me, how to build the ObjectGraph. But I dont quite know whats the best way to do this in my android library projects.
我目前正在尝试将 Dagger 添加到我的 android 项目中。对于应用程序项目,我很容易明白,如何构建 ObjectGraph。但我不太清楚在我的 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? Or should i build the whole ObjectGraph in the library?
我应该继续在应用程序的 Application 类中构建 ObjectGraph 并将 OG 传递给 LibraryModule - 将库的 OG 加到 Apps OG 中吗?还是我应该在库中构建整个 ObjectGraph?
What if I need to inject a class in the library by ObjectGraph.inject(this)
? In my Apps projects I can get the OG from the Application class. But how to handle this in the library? Should I add a @Provides method for the ObjectGraph?
如果我需要在库中注入一个类ObjectGraph.inject(this)
怎么办?在我的 Apps 项目中,我可以从 Application 类中获取 OG。但是如何在图书馆中处理这个问题?我应该为 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)
我无法访问 OG 的库项目,因为它是在应用程序类中构建的?
采纳答案by Edgar Da Silva Fernandes
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);
and that't it.
当库模块中的某个类需要注入其成员时,我只需调用即可 DaggerWrapper.getComponent().inject(this);
。
回答by LaLiLuLeLo
I'm doing this way:
我这样做:
@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
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
How to get ObjectGraph from main project into the library? You could have interface like this:
interface Injector { void inject(Object object); public ObjectGraph getObjectGraph(); }
@Module 类属于主项目,它们提供您要注入到库元素的实现,因此库项目中没有 @Module 类
需要依赖的库元素必须能够访问 ObjectGraph 并调用 .inject() 自己,但主项目应该将 ObjectGraph 实例提供给具有提供的 @Module 依赖的库
如何从主项目获取ObjectGraph到库中?你可以有这样的界面:
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 类这样的上下文对象实现了这个接口(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 接口,并在库项目中为 ActivationModule 创建了具有提供依赖关系的 ObjectGraph。
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 {
....
}
回答by Amir Ziarati
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. (the easier to work with the better practice)
如果您将此库提供给人们,而他们对您的场景一无所知,那么您必须以一种方式编写它,您的 Dagger 可以在没有用户任何帮助的情况下完美运行。(更好的实践越容易工作)
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:
我只是为你写了一些库来展示如何做到这一点。我以一种您甚至可以独立运行它并在消息选项卡中查看结果的方式编写了该库。您的库的用户不需要对 dagger 一无所知,他什么也不做,他只是使用库,dagger 将被配置:
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.
您的所有问题都在这两个项目中得到解答。在评论中提出任何问题和错误的答案。