I have a Core Android Library where I'm defining a CoreComponent ad using the @Singleton scope to inject instances of classes provided by a CoreModule.
@Singleton
@Component(modules = {CoreModule.class})
public interface CoreComponent {
void inject(SomeClass target);
}
@Module
public class CoreModule {
@Singleton
@Provides
CoreRepository provideCoreRepository() {
return new CoreRepositoryImpl();
}
}
I would like to access the same @Singleton instances from another Android Library that is depending on the Core Library and is using another component.
@Singleton
@FooScope
@Component(modules = {CoreModule.class, FooModule.class})
public interface FooComponent {
void inject(SomeActivity target);
}
public class FooActivity extends AppCompatActivity {
@Inject
public CoreRepository repo;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
injectDependencies();
super.onCreate(savedInstanceState);
}
[...]
}
The code above builds but the @Singleton scope is "local" to the Component. In other words there are two singleton instances, one for the for the CoreComponent and one for the FooComponent.
Android Application
├── Foo Library
| └── Core Library
├── Bar Library
| └── Core Library
·
·
·
└── Core Library
I think that the best solution should be using a Subcomponent but, unfortunately, doesn't seem possible because the Core Library has no visibility of the other libraries.
Is there another way to share with Dagger the same instance of one class between components if the class is annotated with the same Scope?