https://kotlinlang.org logo
#android
Title
# android
u

uhe

11/14/2018, 3:07 PM
I keep dagger out of my library modules
👍 1
d

dpk

11/14/2018, 3:10 PM
i regret not doing this. i did some research before i started and found people describing how to do it, and arguing in favor of it, but it gets really ugly. especially when you want to use
internal
classes. i would like an alternative that still lets me use dagger or something dagger-like, ’cause i’d prefer not to go back to manually constructing objects.
u

uhe

11/14/2018, 3:50 PM
Yeah, well. It is a minor inconvenience to manually wire your library classes in your app module, but I can live with that. The library classes themselves look the same anyways (just without any @Inject annotations), ie standard constructor injection.
p

Paul Woitaschek

11/14/2018, 4:05 PM
I really want dagger in my modules as well; some are just too complex to not use dagger. Especially if they need some dependencies from the outside.
I kind of need subcomponents which are generated in the modules where they are declared and which just take the dependencies from their parent component but handle the dependencies that are declared in their modules themselfes because these modules are internal.
u

uhe

11/14/2018, 4:34 PM
no idea if that is a fit for your case, but it sounded quite similar
d

David

11/14/2018, 6:46 PM
I use component-to-component dependencies where each of the gradle-modules has a top level @Component. I can then control what consumer gradle-modules can inject from the library module by exposing it in the Component. This controls the API surface. With this approach you "loose" the ability to use Dagger Scoping in child modules unless you use @Subcomponents (which aren't aware of any parent scopes).
a

agrosner

11/14/2018, 8:31 PM
I just use module per library module. the main app component wires together all of the modules and only modules depending on each other can access the others dependencies
d

David

11/14/2018, 8:35 PM
Yep module-to-module (no components) is another way 👍
a

agrosner

11/14/2018, 8:36 PM
And if you’re worried about exposing something of one module to another, I can abstract with an interface that each module depends on and the providing module supplies the dependency to the consuming module
d

David

11/14/2018, 8:37 PM
Ah interesting so a kind of ModuleComponent hybrid? (As in the
interface
, contract enforcing side of a @Component)
a

agrosner

11/14/2018, 8:38 PM
not sure? but it makes one module not directly depend on another. They indirectly depend on each other,. but that common module between the two might not change often, so they dont need to recompile.
d

David

11/14/2018, 8:45 PM
I see. Would be interested to have a look at that arrangement if you have a repo. It's a shame that there isn't more formal (not just individuals!) advice here. I've just decided that my gradle module library deps can't scope with Dagger in my soln & then hand craft the Double Checks for important scopes like Singleton. Your approach I assume allows you to continue scoping through the tree at the expense of having to build "something around Dagger" for it to work without inadvertently exposing objects through the tree that aren't meant to leave the module?
1
a

agrosner

11/15/2018, 6:20 PM
dont have a public example of that, but heres a multimodule example with a dagger module in a few modules that get tied together by the app: https://github.com/art-institute-of-chicago/aic-mobile-android
d

David

11/15/2018, 7:49 PM
That is a well layered app. Kudos. Looking at https://github.com/art-institute-of-chicago/aic-mobile-android/blob/dev/app/src/main/kotlin/edu/artic/AppComponent.kt it reminds me of how I used to use Dagger 1. 🤔 You've made me think @agrosner...
a

agrosner

11/15/2018, 8:03 PM
thanks! yeah
h

hmole

11/16/2018, 6:25 AM
What if I need to Inject dependency from module B inside module A's platform class(Service, BroadcastReceiver, Worker)? How can you do it without components in library?
r

radityagumay

11/17/2018, 12:17 AM
I did module to module dependencies. Or we can let module has no dagger at all, but let app module wire it by creating dependencies.
u

uhe

11/19/2018, 9:09 AM
Yes, that. In my case the app module creates the entire dependency graph.
p

Paul Woitaschek

11/19/2018, 9:27 AM
That doesn't work for me 😕
I have my
app
gradle module and it depends on a library gradle module
GradleB
. Now my module
GradleB
needs to create a component
DaggerComponentB
. However it has a dependency on a
DaggerModuleC
declared in
GradleModuleC
. Now I can't use a subcomponent for
DaggerComponentB
because the app has no dependency on
GradleModuleC
and it's
DaggerModuleC
.
🤔 2
Dagger Dependency Nightmare
s

sujin

11/25/2018, 6:22 AM
https://github.com/cerberusv2px/android-modular Not sure if it helps.. but i did something like this.. not efficient though
r

radityagumay

11/25/2018, 7:31 AM
2 Views