Annotation Processing in Kotlin Multiplatform can be done with kapt
when one has a jvm target.
But how does one process annotations if there is no jvm target?
Specifically I want to generate code when processing annotations from commonMain
. But I can't figure out how to process those.
My annotation processor just logs at the moment:
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedAnnotationTypes("ch.hippmann.annotation.Register")
@SupportedOptions(RegisterAnnotationProcessor.KAPT_KOTLIN_GENERATED_OPTION_NAME)
class RegisterAnnotationProcessor : AbstractProcessor(){
companion object {
const val KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated"
}
override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment?): Boolean {
processingEnv.messager.printMessage(Diagnostic.Kind.WARNING, "Processing")
return true
}
}
The annotation is in a separate multiplatform module and resides in commonMain
:
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Register
And is used in a project in commonMain
:
part of build.gradle
:
kotlin {
jvm()
// For ARM, should be changed to iosArm32 or iosArm64
// For Linux, should be changed to e.g. linuxX64
// For MacOS, should be changed to e.g. macosX64
// For Windows, should be changed to e.g. mingwX64
linuxX64("linux")
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')
implementation project(":annotations")
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
jvmMain {
dependencies {
implementation kotlin('stdlib-jdk8')
}
}
jvmTest {
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}
linuxMain {
}
linuxTest {
}
}
}
dependencies {
kapt project(":annotationProcessor")
}
This works as log as there is the jvm()
target present. But if i remove it, i can't use kapt
.
So how to process the annotations when there is no jvm
target?