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
java android dagger-2


source to share


No one has answered this question yet

See similar questions:

2
Application context injection in library module with dagger 2

or similar:

3799
How do I read / convert an InputStream to a string in Java?
3606
Close / hide Android soft keyboard
3295
Why is the Android emulator so slow? How can we speed up the development of an Android emulator?
3288
Correct use cases for Android UserManager.isUserAGoat ()?
2609
Is there a unique identifier for an Android device?
2510
How to persist android activity state by persisting instance state?
2284
How can I fix 'android.os.NetworkOnMainThreadException'?
2097
Is there a way to run Python on Android?
784
How do I add a library project to Android Studio?
17
Dagger 2: provide the same instance between multiple components with the same scope on different library modules



All Articles
Loading...
X
Show
Funny
Dev
Pics