Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger injection not working for "object" in Kotlin

After spending a ludicrous amount of time trying to figure out why my dagger injections weren't working; I realised that the "object" type in Kotlin was the problem.

The following did not work, the injected "property" was null.

object SomeSingleton {      @Inject     lateinit var property: Property      init {         DaggerGraphController.inject(this)     } } 

However, the following DID work just fine:

class NotSingleton {      @Inject     lateinit var property: Property      init {         DaggerGraphController.inject(this)     } } 

I tried google, I tried the documentation but I could not pin point the reason behind this. Also note that I havent tried this with JAVA, JAVA doesnt have the concept of singletons built in anyway.

Why is this the case? Why is a kotlin singleton unable to inject members but a regular non-singleton class can?

like image 597
Nihilanth Avatar asked Jan 24 '18 02:01

Nihilanth


2 Answers

If you look into kotlin bytecode you'll find that the code you've written is translated into following:

public final class SomeSingleton {     public static LProperty; property // <- Notice static field here      public final getProperty()LProperty     ...      public final setProperty(LProperty)V     ... } 

As you can see the actual field is static which makes it uneligible for instance injection. You may try to move @Inject annotation onto setter method by doing so:

object SomeSingleton {     @set:Inject     lateinit var property: Property     ... } 
like image 136
SimY4 Avatar answered Sep 23 '22 10:09

SimY4


I tried to use dagger.Lazy<YourClass> and it works

 @set:Inject lateinit var authClient: dagger.Lazy<PlatformAuthClient> 
like image 21
Dennis Zinkovski Avatar answered Sep 24 '22 10:09

Dennis Zinkovski