如何使用匕首一款Android库项目 [英] How to use dagger in a android library project
问题描述
目前,我正在试图匕首添加到我的Android项目。对于应用程序项目的方便,我清楚,如何建立ObjectGraph。但我不完全知道什么最好的办法做到这一点在我的Android库项目。
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.
我要保持建设ObjectGraph在应用程序的应用类并把OG转移到LibraryModule - plussing库的OG的应用OG?或者我应该在图书馆建全ObjectGraph?
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?
如果我需要用 ObjectGraph.inject一类在库中注入(本)
?在我的应用程序项目中,我可以从应用程序类的OG。但如何在图书馆处理呢?我应该添加一个@Provides方法为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(本)
在我的图书馆项目中,我没有访问OG,因为它正在建造中的应用程序类?
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?
推荐答案
我做的是这样的:
-
@Module类属于主体工程,他们提供了你所注入到库元素的实现,所以没有@Module类在库项目
@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
这是预期的依赖必须获得ObjectGraph并调用.inject()在自己身上,但主要的项目应给予ObjectGraph实例库所提供@Module依赖库元素
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?你可以有这样的接口:
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();
}
上下文对象,如活动或应用程序类实现(ObjectGraph对象的持有人)这个接口。
Context objects like Activity or Application class implements this interface (holders of ObjectGraph objects).
如果您有需要的东西,从主体工程,这将是这样的注资库模块的例子活动的:
If you have example of Activity in the library module which needs something to inject from the main project this would look like this:
class LibraryActivity extends Activity {
@Inject ActivationModule instance;
void onCreate(... ) {
Injector injector = (Injector)getApplicationContext();
injector.inject(this)
}
}
ActivationModule是类/接口库项目。
ActivationModule is the class/interface in the library project.
主要项目有它实现喷油器的界面和提供关系是不为ActivationModule库中的项目创建了ObjectGraph应用程序类。
Main project has application class which implements Injector interface and creates ObjectGraph with provided dependecy for ActivationModule in the library project.
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 {
....
}
这篇关于如何使用匕首一款Android库项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!