Reputation: 2667
I'm trying to implement new Instant App feature in my Android app. I have 4 modules:
base
push
installed
instant
Since Instant App doesn't support push notifications, I want to enable push
module only for installed app. But I have shared classes in base
that call push
methods. For example, I initialize push
module during app startup in Application class.
How do I organise dependencies, so I don't have to include push
module to base
module, but being able to call push
methods from base
? I have an idea that I can create interface PushManagerProvider
in base
module so instant
and installed
can provide their own implementations, but I'm not sure how can I inject concrete implementation to base
depending on current module.
Upvotes: 1
Views: 1087
Reputation: 627
Assuming you use dagger and that you build a singleton component in your Application class, here's one approach.
In base
, define an Injector
interface that your @Component
class extends. Have your Application in your base module implement an InjectorProvider
interface that has a single getInjector()
method that returns your constructed component. That component will be built using a module that provides a no-op PushManagerProvider
.
Then in your push
module, create an alternative implementation of your Application
class (extend your base and rename to PushAwareApplication
?) That creates a component using a dagger module that provides a legit PushManagerProvider
and use merge rules to force use of the new PushAwareApplication
name when push is built into your application module.
This way you can call
((InjectorProvider)getApplication()).getInjector().inject(this);
And get a different component implementation depending on whether you built the installable app or the instant app.
There are probably more correct ways of doing this with dagger, but this should get you started.
If you're not using dagger, you can use the same Application override technique described above with both Application implementations implementing an interface that offers a 'getPushManagerProvider()` method.
Upvotes: 2