Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kapt: How to process test sources?

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?

like image 294
Alex Avatar asked Sep 24 '17 20:09

Alex


1 Answers

Some sources (e.g. how to use kapt in androidTest scope) suggest to run gradle kaptTest, but this does not work either.

You misunderstood the suggestion. It says kaptTest should be used in place of kapt in the dependencies block, as described here, not that you should run Gradle differently. OTOH, the documentation also specifically says

Note that kaptAndroidTest and kaptTest extends kapt, so you can just provide the kapt dependency and it will be available both for production sources and tests.

So there indeed seems to be a bug there.

I've linked this question in https://youtrack.jetbrains.com/issue/KT-21891. I didn't want to file a separate bug because I don't know which Gradle/Kotlin/Kotlin Gradle plugin/etc. you had.

like image 163
Alexey Romanov Avatar answered Sep 30 '22 21:09

Alexey Romanov