跨 Gradle 模块使用 Dagger 多重绑定

Oce*_*ife 5 android dependency-injection guice gradle dagger

其他Dagger用户……我是在尝试做一些不可能的事情吗?下面是我的应用程序的degraph生成的 Gradle 模块视图。

模块化-android-app

想象一下我的应用程序有一个导航抽屉。我希望基于通过多重绑定填充的集合添加抽屉中的列表项。这种多重绑定的贡献者跨越 > 1 个 Gradle 模块。例如,从user模块中添加了一个名为“帐户”的列表项,从模块中添加了一个名为“条款和条件”的第二项,legal并从searchGradle 模块中添加了一个“搜索”导航条目。这些模块可以被认为是独立的应用程序,捆绑在一起形成完整的应用程序体验。它们可以自己运行。

关于此的 Dagger 文档看起来像是 Guice 的复制粘贴,但有一个很大的复杂性。它指出;

“Dagger 允许您将多个对象绑定到一个集合中,即使这些对象使用多重绑定绑定在不同的模块中。”

...但这意味着 Dagger @Modules,对吗?是否可以跨 Gradle 模块填充多重绑定?我已经尝试过类似的东西,但这不是我所期望的(并非所有的贡献都被收集了);

父应用

@Module
abstract class PluginModule {
  @Multibinds @NavigationContribution abstract Set<NavigationEntry> navigables();
}
Run Code Online (Sandbox Code Playgroud)

legal 包含 Dagger @Module 的 Gradle 模块

@Module
class LegalModule {
  @Provides
  @NavigationContribution 
  @IntoSet
  static NavigationEntry provideLegalNavigation() {
    return ...;
  }
}
Run Code Online (Sandbox Code Playgroud)

user 包含 Dagger @Module 的 Gradle 模块

@Module
class AccountModule {
  @Provides
  @NavigationContribution 
  @IntoSet
  static NavigationEntry provideAccountNavigation() {
    return ...;
  }
}
Run Code Online (Sandbox Code Playgroud)

在运行应用程序时,调用时只有应用程序“上下文”下的贡献可用;

@Inject @NavigationContribution Set<NavigationEntry> navigationEntries();
Run Code Online (Sandbox Code Playgroud)

其他贡献可以通过获取dagger.Component子 Gradle 模块的 并公开一个方法来获取条目来“手动”访问。

这违背了我的应用程序的多重绑定器的目的。这可能吗?如果不是,为什么?

更新:与杰夫分流

在此处输入图片说明

因此,顶级模块注释看起来像这样;

@ApplicationLifecycle
@Component(
    modules = {
        OceanLifeModule.class,
        AddSpotActivityModule.class,
        ModelModule.class,
        PluginModule.class
    },
    dependencies = {
        NavigationComponent.class,
        LegalComponent.class,
        PreferenceComponent.class,
        DomainComponent.class
    }
)
Run Code Online (Sandbox Code Playgroud)

Oce*_*ife 2

@jeff-bowman - 我在这里缺少的includes = {}PluginModule. Zac Sweers 发表的关于同一主题的博客文章我指明了正确的方向。