https://kotlinlang.org logo
#dagger
Title
# dagger
r

rattleshirt

06/11/2020, 11:37 AM
Wrt Hilt: how does the
@InstallIn
work inside gradle submodules? Would you need a sub component per module? I couldn’t find anything in the official doc regarding this.
j

Javier

06/11/2020, 11:38 AM
You don't need to create components, just modules
r

rattleshirt

06/11/2020, 11:39 AM
What if that module is inside a gradle submodule?
@InstallIn
requires access to the
ApplicationComponent::class
m

Manuel Vivo

06/11/2020, 11:39 AM
If
@InstallIn
is in the transitive dependencies of the module that contains the
@HiltAndroidApp
, that module will be automatically picked up and installed in the component you specified.
r

rattleshirt

06/11/2020, 11:39 AM
Without actually specifying the
ApplicationComponent
?
m

Manuel Vivo

06/11/2020, 11:40 AM
You need
ApplicationComponent
r

rattleshirt

06/11/2020, 11:43 AM
Ah, my bad.
ApplicationComponent
is actually provided by Hilt. 🤦
So just adding the hilt-android dependency to the submodule would be enough
But only android submodules I guess?
m

Manuel Vivo

06/11/2020, 11:45 AM
Correct
r

rattleshirt

06/11/2020, 11:45 AM
Alright, thank you
👍 1
j

Javier

06/11/2020, 11:45 AM
I think so, because that I have a specific android module for di
r

rattleshirt

06/11/2020, 11:45 AM
Yeh, makes sense since it is android focused
@Module(includes=)
would do the trick for non-android submodules
j

Javier

06/11/2020, 11:48 AM
I have to try, I like your idea if it works
m

Manuel Vivo

06/11/2020, 11:50 AM
The only problem I see with that is that in tests, you cannot uninstall a module that is included in other module. If you don’t need to uninstall it, that sounds great
j

Javier

06/11/2020, 12:23 PM
But you just uninstall the module which has the InstallIn annotation no? You don't need to uninstall the module inside the kotlin module no?
w

wasyl

06/11/2020, 8:32 PM
So what’s recommended alternative for non-android modules? Having a separate component? A module and a separate module somewhere where I already have android framework dependency? (*I haven’t tried Hilt yet, so I might not know something obvious)
m

Manuel Vivo

06/11/2020, 10:02 PM
Including Dagger modules that are in non-Android gradle modules from a Hilt module (that is in an Android module) installed in the right component is the best practice we found so far. There are some downsides like not being able to uninstall those Dagger modules from tests but... If you don't need that, it should be enough. You could also have scoping working using the
@AliasOf
annotation
w

wasyl

06/12/2020, 9:14 AM
Gotta say this is highly disappointing. I understand Hilts goal was making
android
development easier, but ignoring common and useful use case of having non-android modules is not that helpful, and will only introduce inconsistency in the project. And once Hilt is stable and it’s not addressed (and I don’t have any hopes at this point) it’s gonna be same thing as with https://issuetracker.google.com/issues/123071304, so “it’s too late now, let’s make this change never”
2
j

Javier

06/12/2020, 9:55 AM
I tried Kotlin Multiplatform libraries and I think they can be a nice way to solve this problem. For example I want to create a library called "core". Al Kotlin pure code will be in commonMain, the JVM in the jvmMain and the android in androidMain. Then if I add this core to a kotlin JVM module
implementation("...:core")
I don't need to put
-jvm
to get the jvm, and if I write the same snippet in an Android module, I don't need to add the
-android
(or another suffix) to get it. So using the same
implementation(...:core)
in all modules is easier than having to think if I have to add multiple implementation or just one.
9 Views