Kotlin Error: Dagger does not support injection into private fields Kotlin Error: Dagger does not support injection into private fields android android

Kotlin Error: Dagger does not support injection into private fields


You have mistake here:

    @Injectinternal var mSharedPreferences: SharedPreferences? = null

This looks like you added @Inject annotation to the KotlinFragment class

Please change it to this and it will work:

var mSharedPreferences: SharedPreferences? = null        @Inject set

Here is the link to the documentation: https://kotlinlang.org/docs/reference/annotations.html


Accidentally I came across to my own answer and have to confess, that in fact it isn't working (at least for my use-case). Please consider Avilio's answer which worked for me also: substitute internal with lateinit.


Old answer

Remove internal modifier. Dagger needs at least package-private access in order to access annotated field. In Kotlin internal modifier is not a substitution for Java's package-private access modifier.

For detailed explanation of differences between modifiers in Java and Kotlin refer to Fragmented podcast's episode #101 - "Learning Kotlin – visibility modifiers, internal modifier, modules", as well as the official docs.


As simple you can do this, change this line

@Injectinternal var mSharedPreferences: SharedPreferences? = null

To

@set:Injectinternal var mSharedPreferences: SharedPreferences? = null

this work like charm in my case.