6

I have a Core Android Library where I'm defining number of interfaces and its default implementations. All of them are exposed by CoreModule which is defined in CoreComponent.`

Core module

@Module
public class CoreModule {

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

Core component

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

All implementations are injected only in Core library.

There is also a Ui library which is dependent on Core library. Additionally, Ui library can provide a custom implementation to some of interfaces defined in Core library.

├── Ui Library
  └── Core Library
└── Core Library

In order to achieve the goal I have created a custom @component and @module in 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 that the best solution to provide a custom implementation to Core library is be using a Subcomponent but, unfortunately, it doesn't seem possible because the Core Library has no visibility to the other libraries (Ui library and it's @component) , so I can't inject any of custom implementations in Core library. Any help?

empi
  • 15,755
  • 8
  • 62
  • 78
user2692429
  • 115
  • 1
  • 6
  • dagger 2 is a very conservative design idea, what exactly is your question, as in links etc – Remario Apr 23 '17 at 21:11
  • In order to have an access to any custom implementation in Core library, I have to create a instance of UiComponent in Core library, right? However Core Library has no visibility to the other libraries (Ui library and it's @component). So how I can provide custom implementation to Core library? – user2692429 Apr 23 '17 at 21:17
  • 1
    Did you find a solution ? I have exactly the same problem. – Lyofen Jun 26 '17 at 12:35
  • You might be interested in this solution: https://stackoverflow.com/questions/47929260/injecting-application-context-in-library-module-with-dagger-2?noredirect=1#comment85206110_47929260 – IgorGanapolsky Mar 05 '18 at 00:43

0 Answers0