Using AndroidInjector (Dagger 2.10) to inject a Service inside a library

362 views Asked by At

According to dagger.android we could use AndroidInjector to inject any Android component, in this case I'm looking to inject a Service using either DaggerIntentService or calling directly AndroidInjection.inject(this) in my service. I understand that we need to setUp a subComponent for the service like:

@Subcomponent(modules = ...)
public interface MyServiceSubcomponent extends AndroidInjector<MyService> {
  @Subcomponent.Builder
  public abstract class Builder extends AndroidInjector.Builder<MyService> {}
}

The question is that in all the examples in order to be able to user the AndroidInject, we need to build the main component in the application and then Dagger does the rest. My problem is that since my Service is inside a library project I do not have an Application class to add the final wiring needed by these components. This can be achieved using normal Dagger2 setUp adding an inject(MyService service) to my library component and the wiring directly on my onCreate but I would like to use the new components, is this possible?

0

There are 0 answers