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

Colton Idle

03/14/2020, 2:25 PM
I updated from dagger 2.21 to dagger 2.27 and now I see errors when compiling
error: Dagger does not support injection into Kotlin objects
I'm admittedly someone still very new to dagger and so I'm not sure if I'm violating some overarching DI rules with trying to inject my moshi instance into my "util object". Can someone point me in the right direction of a solution? I suppose I can write extension methods instead of having this java util that was converted to a kotlin object?
p

Paul Woitaschek

03/15/2020, 9:47 AM
Show the code that fails
s

Sinan Kozak

03/15/2020, 10:10 AM
According to release notes, it is not allowed any more https://github.com/google/dagger/releases
c

Colton Idle

03/16/2020, 3:23 AM
@Sinan Kozak do you happen to know what the best workaround is for this?
s

Sinan Kozak

03/16/2020, 7:23 AM
Nope, i never injected in object class.
p

Paul Woitaschek

03/16/2020, 10:24 AM
@Colton Idle You could use a dependency holder class
I.e.
Copy code
object MySingleton{

  class Deps @Inject constructor(
    val depA : Int
  )

  lateinit var deps : Deps
}

fun initSingleton(){
  MySingleton.deps = component.deps
}
But youre better of just using class and let dagger manage the singletons in the first place
j

Jeremy

03/16/2020, 1:50 PM
Yes, you probably want
@Singleton
c

Colton Idle

03/17/2020, 1:47 PM
So I need to convert all of my old school style Java util classes/methods to a singleton and then inject that everywhere? Hrm. Not in love with that idea, but I guess it'll work. Maybe I could get away with what @Paul Woitaschek said for now. I don't want to make a bunch of changes to other classes that just so happen to use different utils.
p

Paul Woitaschek

03/17/2020, 1:52 PM
Then keep in in java
☝️ 1
j

Jeremy

03/17/2020, 8:35 PM
Dagger works equally well w/ Kotlin and Java but if you want Dagger to manage your Singleton dependencies need to make some minor changes
Singleton
is not the same as
static
and kotlin
object
c

Colton Idle

03/18/2020, 3:51 PM
Understood, it's just that I have a static util, and I'm not trying to make that static util class a
@Singleton
. I'm instead trying to inject a singleton into that util so I can make sure it uses the same moshi instances. I guess at this point I'll just reevaluate the whole thing.
j

Jeremy

03/18/2020, 4:49 PM
Yah I'd avoid using
object
singletons to maintain global state but if you wanted to retain it you'd probably manually inject the moshi
If nothing else it will be difficult to test
c

Colton Idle

03/19/2020, 1:33 AM
The
object
at hand doesn't handle any global state. It's just contains 4 util methods that rely on moshi to convert some json payloads to certain types.
s

Sinan Kozak

03/19/2020, 6:28 AM
Create moshi in util class provide it to dagger from util class.
p

Paul Woitaschek

03/19/2020, 9:47 AM
Then Moshi is the global state
If it's only Moshi use a by lazy and get Moshi from the component
c

Colton Idle

03/19/2020, 3:07 PM
Hm. This is all really going over my head. 😞 I might play around with this later. Appreciate all of the help. I think I might try to get rid of my util and just make extension functions and see if that helps.
8 Views