【发布时间】:2020-05-04 04:10:24
【问题描述】:
当有 jvm 目标时,Kotlin Multiplatform 中的注释处理可以使用 kapt 完成。
但是如果没有 jvm 目标,如何处理注释呢?
具体来说,我想在处理来自commonMain 的注释时生成代码。但我不知道如何处理这些。
我的注释处理器此时只是记录:
@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
}
}
注解位于单独的多平台模块中,位于commonMain:
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Register
并在commonMain的项目中使用: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")
}
这与存在 jvm() 目标时的日志一样。但是如果我删除它,我就不能使用kapt。
那么当有nojvm目标时,如何处理注解呢?
【问题讨论】:
标签: kotlin annotations annotation-processing kotlin-multiplatform kapt