Android Dagger2实现多个接口的实体的多模块注入

Android Dagger2实现多个接口的实体的多模块注入,android,dagger-2,dagger,multi-module,Android,Dagger 2,Dagger,Multi Module,假设我们的应用程序中有以下模块(箭头表示取决于): app->{module1,module2}->{core module} 我们的模块1定义了Interface1,我们的模块2定义了Interface2。我们的应用程序模块将Interface1n2实现(实现Interface1和Interface2)定义并实例化为singleton 问:就dagger2生态系统而言,如何为module1和module2提供相同的Interface1n2实现实例?您的AppModule可以创建具有特定范围的实

假设我们的应用程序中有以下模块(箭头表示取决于):

app->{module1,module2}->{core module}

我们的模块1定义了Interface1,我们的模块2定义了Interface2。我们的应用程序模块将Interface1n2实现(实现Interface1和Interface2)定义并实例化为singleton


问:就dagger2生态系统而言,如何为module1和module2提供相同的Interface1n2实现实例?

您的
AppModule
可以创建具有特定范围的实现(
@Singleton
在本例中),您可以有2个提供方法返回此实现实例

注意:未经测试

@Module
object AppModule {
    @Provides
    @Singleton
    internal fun provideImplementation() : Interface1n2Implementation = 
    Interface1n2Implementation()

    @Provides
    fun provideInterface1(implementation: Interface1n2Implementation) : Interface1 = implementation

    @Provides
    fun provideInterface2(implementation: Interface1n2Implementation) : Interface2 = implementation
}

谢谢,测试。不是开箱即用,但我认为这个想法应该是活的,让我调试好它。