How to use dagger 2 in android library project

I have a base Android library in which I define the number of interfaces and its default implementations. All of them are rendered using the CoreModule, which is defined in the CoreComponent. `

The main module

@Module
public class CoreModule {

@Provides
public IAuthenticationViewExtension provideToolsViewExtension() {
    return new AuthenticationViewExtension();
}
@Provides
public ISettingsViewExtension provideISettingsViewExtension() {
    return new SettingsViewExtension();
}
}

      

Main component

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

      

All implementations are injected only into the Core library.

There is also a Ui library that depends on the Core library. In addition, the Ui library can provide a custom implementation for some of the interfaces defined in the Core library.

β”œβ”€β”€ Ui Library
  └── Core Library
└── Core Library

      

To achieve the goal, I created a custom @component and @module in the Ui library

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

@Module
public class DemoUiModule {
@Provides
public IAuthenticationViewExtension provideToolsViewExtension() {
    return new CustomAuthenticationViewExtension();
}
}

      

I think the best solution to provide a custom implementation of the Core library is to use a subcomponent, but unfortunately this is not possible because the main library is not visible to other libraries (Ui and @component library), so I cannot enter which -or custom implementations in the Core library. Any help?

+3


source to share





All Articles