How it's supposed to use Anvil in multi module Android projects

787 Views Asked by At

I'm going to use Anvil in my project.

https://github.com/square/anvil

But I can't figure out what is desired model of usage for it? Should I have a single global AppScope and merge all bindings from every :core and :feature module into it? Thus, every :feature module adds it's bindings into AppScope, thus every :feature knows everything about any other :feature. This sounds messy, am I right?

I've not seen any possibility to have dependent scopes. For example, MyFeatureScope that depends on the AppScope. So only particular :feature module actually contributes it's bindings into MyFeatureScope, but they still could use general-purpose bindings from AppScope.

  • does Anvil supports such dependencies between it's Scopes?

  • should I have one global AppScope? Or should I have a set of isolated scopes - one per :feature module - that could somehow depend on AppScope with "common bindings"?

1

There are 1 best solutions below

0
On

Ultimately it's up to you to decide how to structure your application. Anvil supports multiple scopes and components, precisely like vanilla Dagger 2.

does Anvil supports such dependencies between its Scopes?

Scopes do not "depend" on each other. Components (and subcomponents) do. Anvil does support these dependencies.

should I have one global AppScope? Or should I have a set of isolated scopes - one per :feature module - that could somehow depend on AppScope with "common bindings"?

Do whatever makes the most sense to you. In my application, I use different components to separate pure Kotlin classes and Android-dependent classes.

Let me know if you'd like me to elaborate or demonstrate code samples.