Posts

Showing posts from January 21, 2019

Can I use Dagger 2's field injection in Kotlin?

Image
up vote 12 down vote favorite I posted a question (Dagger 2 does not generate the component class (Android, Kotlin)), and after some experiments, it seems that the problem might be due to that Kotlin hides the field. class CoffeeShop { @Inject var theCoffee: Coffee? = null } The error message is, :app:kaptDebugKotline: ...CoffeeShop.java:7: error: Dagger does not support injection into private fields e: private ....Coffee theCoffee; theCoffee was not private in my source code. But I think Kotlin may be translating class CoffeeShop { @Inject var theCoffee: Coffee? = null } into Java code of class CoffeeShop { @Inject private Coffee theCoffee = null; public Coffee getTheCoffee(); public void setTheCoffee(); } Can I use field injection in Kotlin?