The error (Dagger does not support injection into private fields) will appear when you attempt to inject a value into a private field. The error condition itself is OK and understandable but the error message does not include any context, making it can be difficult to track down exactly which field is (fields are) affected. In my particular case one erroneous field that doesn't even appear to be private is causing 9 such error messages in a row, making it extra difficult to figure out what the hell is going on. (This ticket isn't reporting that as an issue, only the error message itself).
I am using Dagger 2.8.
When this error occurs, Dagger provides the offending element to the compiler who is responsible for rendering it in a way that points to the source. You see this in normal javac errors, but it seems like a bug in the compiler that it doesn't provide context in the source.
I have seen this come up quite a bit for Dagger's errors as well as other processors.
Bummer. Not sure who/what to report this to. For now, my "workaround" is to modify a local checkout of dagger to write out useful information to a file.
Does this error cross compilation units? Javac often doesn't have the right information when that happens
I suspect that this is not an issue with Dagger or javac, but the build tool that is wrapping them. I know at one point Maven had some issues with how it peeled output out of javac and presented it. Which build system are you using? Can you give an example of how exactly it is presented?
If I understand correctly I am not crossing compilation units. I'm using Gradle with mostly default settings provided when creating a new Android Studio project.
I've uploaded a barebones example here:
https://github.com/dpkirchner/dagger-issue-551
Assuming you have AS and the Android SDK installed, when you clone that and run ./gradlew :app:testDebug and you'll see:
:app:compileDebugUnitTestKotlin
:app:compileDebugUnitTestJavaWithJavac
Destination for generated sources was modified by kapt. Previous value = /Users/dpk/Documents/git/Demo2/app/build/generated/source/apt/test/debug
error: Dagger does not support injection into private fields
error: Dagger does not support injection into private fields
error: Dagger does not support injection into private fields
error: com.example.demo.FooTest cannot be provided without an @Inject constructor or from an @Provides-annotated method.
com.example.demo.FooTest is injected at
com.example.demo.FooTest.Component.inject(arg0)
If you remove the annotations from the inject statement the dagger component will be generated successfully and the test will run (it will fail because it throws an exception, but eh).
In this extremely contrived example I'm not actually testing any code -- the issue appears to be isolated to the test module (hence my guess that I'm not crossing compilation units).
Edited to add: I should probably mention that I'm using that @field:[Named(...)] notation deliberately. Unfortunately, I don't remember exactly why I found it necessary; I didn't comment it in my original code. Regardless, the same issue exists with it or without it.
I'm not familiar with actually using Kotlin, but I know kapt still has some rough edges. I wonder if this is bad coordination with javac somehow?
A java version of the same* test code does include the error's context using the same configuration. Would seem to point to Kotlin or kapt.
* I had to make someBoolean explicitly private to trigger the error. I guess the Dagger/Kotlin/kapt is not reading the flags right. In any case, that's a separate issue.
Yeah, this rings a bell. I think people have had this sort of trouble with kapt in the past. It's probably worth filing a bug with them about it. Closing this for now as I don't think there's much we can do aside from calling the right processing apis with the right values.
Field injection can be done in Android kotlin by below way.
var mSharedPreferences: SharedPreferences? = null
@Inject set
Also need to add below lines of code in app level build.gradle
apply plugin: 'kotlin-kapt'
kapt {
generateStubs = true
}
implementation 'com.google.dagger:dagger:2.10'
kapt 'com.google.dagger:dagger-compiler:2.10'
Most helpful comment
Yeah, this rings a bell. I think people have had this sort of trouble with kapt in the past. It's probably worth filing a bug with them about it. Closing this for now as I don't think there's much we can do aside from calling the right processing apis with the right values.