2

I have a Kotlin Multiplatform project built with Gradle (.kts). One of the targets is jvm().

For that target I also have a Java library with custom annotations that I need to use. However, when I just put these annotations in my Kotlin code, as far as I can tell, they don't get processed. I am assuming the problem is that the library custom annotation processor is not registered and does not run on my Kotlin code. At the same time,

The question is: how do I register it?

  • I tried setting javac -processor argument, but I couldn't find a way to add it from Gradle build file. The best I could do was the deeply buried compilations.getByName("main").compilationOptions.options.freeCompilerArgs, but it turned out to be Kotlin compiler arguments.
  • I looked for java-specific build features, like annotationProcessor from another question. They are nowhere to be found since I don't have a dedicated Java plugin and/or source set.
  • I found -Xjavac-arguments parameter of kotlinc-jvm, but not only is it experimental (basically a developer tool), but it is also apparently just broken and does nothing.
  • I tried random placements of META-INF/services/javax.annotation.processing.Processor as described in this guide, which quite expectably didn't work either.

At the same time, JUnit's @Test annotations work without any problems, so it should be doable. What am I missing?

UPD: I found that jvm().dependencies does have annotationProcessor() in it, and I also saw that META-INF of the library I'm using does have the necessary processor declared. Although this seems like a proper solution, I'm still getting the same failing behavior. How can I verify that the library processor ran or not?

DL33
  • 194
  • 8
  • You have to use [kapt](https://kotlinlang.org/docs/kapt.html). `@Test` is a different story, I assume it doesn't use annotation processors. – broot Aug 08 '23 at 03:08
  • @broot I have tried it now, but I'm struggling to provide the processor to kapt. The kapt("groupId:artifactId:version") line from the guide seems to not compile... And kapt.javacOptions seem to do literally nothing. – DL33 Aug 08 '23 at 23:55
  • If I see no files under build/generated/, just empty folders, would it be correct to assume the processor didn't run? Is there any chance that the other library's processor output its files into a wrong directory? – DL33 Aug 08 '23 at 23:57
  • I have accidentally discovered kapt.annotationProcessor() which seems to be at least doing something, because if I provide it with a wrong string instead of fqName of processor, it notices that there is no processor. And when I provide it with a correct fqName, it is silent. So if the processor is registered, could it somehow be that kapt didn't run it on my files? Once again, checking if the processor ran at all would be helpful – DL33 Aug 09 '23 at 00:25

0 Answers0