I have a project which uses kapt for annotation processing and code generation (based on the annotations). It works in the main sources, but not in the test sources.
Some sources (e.g. how to use kapt in androidTest scope) suggest to run gradle kaptTest
, but this does not work either. It reports the task as "Up to date", even directly after a clean. Maybe this suggestion is unique to android.
I downloaded the example project from https://github.com/JetBrains/kotlin-examples/tree/master/gradle/kotlin-code-generation, added an annotation usage in the test sources and I got the same behaviour there. It works well for the main sources and it does not work for the test sources.
The only odd thing in the output is:
> Task :example:compileKotlin
Using kotlin incremental compilation
w: [kapt] Sources output directory is not specified, skipping annotation processing
but as you can see, this is not the test sources, but the main sources, and the annotation processor is applied on those.
I added a printout in the annotation processor. It does show up for compileKotlin, but not for compileTestKotlin.
So, what is the magic trick to make kapt work on test sources?
PS: I guess someone is going to ask for my build.gradle. It is identical to the one from the example I linked, so if you can make it work for the example, I can integrate into my buildfile.
PPS: Only similar question I found is Kotlin's kapt plugin for gradle does not work for custom source set (JMH), and they suggest to issue a bug report. So maybe it is a bug in the kapt-gradle-plugin?