Android 在多个库项目中共享依赖项的Dagger组织?
跨库项目共享依赖关系的最佳方式是什么?我希望保持它们的独立性,只是有一些东西明确地告诉组件它需要什么,以及它将在内部提供什么模块Android 在多个库项目中共享依赖项的Dagger组织?,android,dagger-2,dagger,Android,Dagger 2,Dagger,跨库项目共享依赖关系的最佳方式是什么?我希望保持它们的独立性,只是有一些东西明确地告诉组件它需要什么,以及它将在内部提供什么模块 我可以让所有库都提供一个父应用程序可以添加到其组件中的模块,但如果多个模块提供相同的内容,Dagger将(正确地)出错。我想我已经解决了: 库模块提供了一个依赖接口DependencyInterface,以满足它们的需求。在内部,他们将使用自己的组件,该组件依赖于DependencyInterface 集成应用程序只需提供自己的接口“实现”。如果他们自己正在使用Dag
我可以让所有库都提供一个父应用程序可以添加到其组件中的模块,但如果多个模块提供相同的内容,Dagger将(正确地)出错。我想我已经解决了: 库模块提供了一个依赖接口
DependencyInterface
,以满足它们的需求。在内部,他们将使用自己的组件,该组件依赖于DependencyInterface
集成应用程序只需提供自己的接口“实现”。如果他们自己正在使用Dagger,那么AppComponent
将只实现接口,并让Dagger提供依赖项
例如:
库组件端:
@Component(
modules = {
// your internal library modules here.
},
dependencies = {
LibraryDependencies.class
}
)
public interface LibraryComponent {
// etc...
}
public interface LibraryDependencies {
// Things that the library needs, etc.
Retrofit retrofit();
OkHttpClient okHttpClient();
}
对于集成应用程序端:
@Singleton
@Component(
modules = {
InterfaceModule.class,
// etc...
}
)
public abstract class IntegratingAppComponent implements LibraryDependencies {
// etc...
}
/**
* This module is just to transform the IntegratingAppComponent into the interfaces that it
* represents in Dagger, since Dagger only does injection on a direct class by class basis.
*/
@Module
public abstract class InterfaceModule {
@Provides
public static LibraryDependencies providesLibraryDependencies(IntegratingAppComponent component) {
return component;
}
}
你有这样的例子吗?“你的解决方案看起来对我有帮助。”superjugy添加了一个小例子。