Kotlin Multiplatform Annotation Processing
Asked Answered
C

1

7

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?

Causeuse answered 17/1, 2020 at 9:27 Comment(0)
P
1

You seem to have solved your issue, but if anyone ends up here...

I could use kapt on common code without any issues, like this:

val commonMain by getting {
    dependencies {
        ...
         configurations.get("kapt").dependencies.add(project(": annotationProcessor"))
    }
}

It processes annotation on commonMain code without a problem. I do have an android target, though, so if the issue only surfaced when there was no jvm target at all, then ymmv. There are also annotation processors working for Kotlin Native e.g. https://github.com/Foso/MpApt.

Possing answered 25/1, 2021 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.