如何在android库项目中使用dagger 2

use*_*429 6 java android dagger-2

我有一个核心 Android 库,我在其中定义了接口数量及其默认实现。所有这些都由 CoreComponent 中定义的 CoreModule 公开。

核心模块

@Module
public class CoreModule {

@Provides
public IAuthenticationViewExtension provideToolsViewExtension() {
    return new AuthenticationViewExtension();
}
@Provides
public ISettingsViewExtension provideISettingsViewExtension() {
    return new SettingsViewExtension();
}
}
Run Code Online (Sandbox Code Playgroud)

核心组件

@Component( modules = {CoreModule.class})
public interface CoreComponent {
CoreComponent coreComponent();
void inject(BaseLauncher baseLauncher);
ISettingsViewExtension iSettingsViewExtension();
}
Run Code Online (Sandbox Code Playgroud)

所有的实现都只注入到核心库中。

还有一个依赖于 Core 库的 Ui 库。此外,Ui 库可以为 Core 库中定义的一些接口提供自定义实现。

??? Ui Library
  ??? Core Library
??? Core Library
Run Code Online (Sandbox Code Playgroud)

为了实现目标,我在 Ui 库中创建了自定义 @component 和 @module

@Component(dependencies = {UiComponent.class}, 
modules = {UiModule.class})
public interface UiComponent {
}

@Module
public class DemoUiModule {
@Provides
public IAuthenticationViewExtension provideToolsViewExtension() {
    return new CustomAuthenticationViewExtension();
}
}
Run Code Online (Sandbox Code Playgroud)

我认为为核心库提供自定义实现的最佳解决方案是使用子组件,但不幸的是,这似乎不可能,因为核心库对其他库(Ui 库和它的 @component)不可见,所以我无法在 Core 库中注入任何自定义实现。有什么帮助吗?