Using Kotlin with Dagger 1

I Admit that working with Dagger1 is not the best practice since Dagger2 came out. But still there is huge projects that still using it in a wide scope so it is to complicated to migrate into Dagger2.

After a few days of struggling with Dagger base junit test using the new shiny language “Kotlin” I came to few insights:

Problem №1 : Fields Injection

Error : “Dagger does not support injection into private fields”

Dagger use late injection of fields. from the other hand Kotlin compiler try to fix you own code and convert every public member into private member if it doesn’t recognize the external use of this field. this is a very big problem since Dagger1 is not supporting “private fields injection.

The solution : add “lateinit” tag before the member declaration.

Problem №2 : fields vs. getters

Kotlin tend to recognize fields as “getter” of this fields. so is it safer to annotate the fields as “@field:Inject” instead of just simple “@Inject”

for instance :

lateinit var ServiceManagerSuperDuper servicemanager

Problem №3 : Any.class != Object.class

Error : Error:Modules must not extend from other classes:

Dagger use type comparison for modules as part of is reflection tasks. the compilation of Kotlin use “Any” type instead of some “Object” instances. the result is compilation failure for modules classes (the providers).

I found a few blogs and forums posts which claim that this problem was solved in java 8 compilers such as Jake. since google kill Jake (R.I.P) and the new Java 8 compiler still in beta version we must give up for now Kotlin for the modules itself and write it in Java.