Dagger can not find classes generated by other annotation processor
New answer I have somehow missed that you are using kapt. Kapt can process your classes, even without full qualified name (which is remarkable) if you add this to your build.gradle:
kapt {
arguments {
arg("argumentIncremental", 'true')
}
correctErrorTypes = true
}
More info about this: https://kotlinlang.org/docs/reference/kapt.html#non-existent-type-correction
Previous answer can be useful is someone has the same issue with annotationProcessor (apt) in gradle.
Short answer: use fully qualified name for ActivityInjectorModule:
@dagger.Component(modules = {dagger.android.AndroidInjectionModule.class, com.mallaudin.daggietest.di.AppModule.class, com.mallaudin.daggietest.di.ActivityInjectorModule.class})
Alternatively put both files in the same package.
Long answer: Dagger is an annotation processor, it runs before your code is compiled and (potentially) before your other annotation processor runs. The sequence in which processors run is not defined.
Dagger annotation processor will process the TypeElement annotated with @dagger.Component and it will try to find all modules including the "ActivityInjectorModule.class". The thing is, ActivityInjectorModule might not have been generated yet. Therefore "ActivityInjectorModule" will not have a package at this point. Dagger will assume that ActivityInjectorModule resides in the same package as the Component class and will not add an import. The usual workaround for this is to use full-qualified names for generated classes, if they are used by other annotation processors. Sometimes it makes sense to move annotation processing to a difference gradle module, but I don't this that this is what you want.
Solution:
- Generate java code.
Kapt
does not support multiple rounds. - Write generated files on earliest possible round.
Explanation:
Javac
annotation processor uses rounds instead of defining processors order. So normally the simplified algorithm is like that:
- Gather all java sources
- Run all annotation processors. Any annotation processor can generate new files using Filer.
- Gather all generated files and if there are any, run step 2 again.
- If there are no files generated, run one more round where RoundEnvironment.processingOver() returns
true
, signaling this is the last round.
Here is a pretty good explanation of the process
Now a bit about kapt
. Kapt
uses javac to run annotation processors. To make it possible, it runs kotlin compliler first to generate java stub files and runs javac
on them. Currently kapt
does not support multiple rounds, meaning it does not generate java stubs for kotlin classes, generated by annotation processors.
Note: javac
still uses multiple rounds, it just can't pick up generated kotlin sources.
So, back to your question. One possible option is to move your generated classes into a separate module like it's described here.
But the easiest option is to generate java code directly and your generated java classes will be picked up by javac
automatically, launching second round of annotation processing, where dagger will process them.
Just a few more notes:
- Do not generate your code when
RoundEnvironment.processingOver() == true
, it will not trigger another round. Generate it during the same round you see the annotation. - To make the generated code visible to annotation processor, write it using Filer.