无法在 Kotlin 中构建注释处理器
Failed to build annotation processor in Kotlin
我目前正尝试在 Kotlin 中为 Android 编写注释处理器。项目结构如下:
/annotation
/src/main/kotlin/<package>
Annotation.kt
AnnotationProcessor.kt
/sample
project/build.gradle
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.31"
}
}
annotation/build.gradle
apply plugin: 'kotlin'
sourceCompatibility = 1.7
targetCompatibility = 1.7
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"
}
sample/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"
implementation project(':annotation')
kapt project(':annotation')
}
Annotation.kt
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.SOURCE)
annotation class Annotation(val comment: String = "")
AnnotationProcessor.kt
class AnnotationProcessor : AbstractProcessor() {
override fun process(annotations: MutableSet<out TypeElement>?, roundEnvironment: RoundEnvironment?): Boolean = true
}
使用 kapt,构建在 sample:javaPreCompileDebug 上中断,并显示以下消息:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':sample:javaPreCompileDebug'
Caused by: java.lang.RuntimeException: Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor. Please add them to the annotationProcessor configuration.
- annotation.jar (project :annotation)
Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true to continue with previous behavior. Note that this option is deprecated and will be removed in the future.
See https://developer.android.com/r/tools/annotation-processor-error-message.html for more details.
当我用 annotationProcessor 替换 kapt 时,构建在 sample:transformClassesWithInstantRunForDebug 中断并显示以下消息:
Caused by: java.io.IOException: Failed to find byte code for javax/annotation/processing/AbstractProcessor
at com.android.build.gradle.internal.incremental.AsmUtils.lambda$static[=17=](AsmUtils.java:89)
at com.android.build.gradle.internal.incremental.AsmUtils.loadClass(AsmUtils.java:307)
at com.android.build.gradle.internal.incremental.AsmUtils.readClassAndInterfaces(AsmUtils.java:165)
at com.android.build.gradle.internal.incremental.AsmUtils.loadClass(AsmUtils.java:278)
at com.android.build.gradle.internal.incremental.IncrementalVisitor.instrumentClass(IncrementalVisitor.java:342)
at com.android.build.gradle.internal.transforms.InstantRunTransform.transformToClasses2Format(InstantRunTransform.java:406)
at com.android.build.gradle.internal.transforms.InstantRunTransform.lambda$doTransform(InstantRunTransform.java:268)
at com.android.build.gradle.internal.transforms.InstantRunTransform.lambda$null(InstantRunTransform.java:297)
at java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1424)
... 4 more
当我禁用 Instant 运行 时,一切正常。
我现在的问题是,我的配置哪里出错了?我遵循了像 this 这样的示例项目,我看到的唯一大区别是注释模块分为两个(运行时和编译器)。尽管错误消息表明 Kotlin 或 Instant 运行.
存在问题
您必须将 "annotation" 项目分成两部分。
示例:
implementation project(':annotation-lib') // here you put Annotation.kt
kapt project(':annotation-compiler') // AnnotationProcessor.kt
不要忘记将处理器添加到 annotation-compiler 项目中的 META-INF,请参阅 What is the default annotation processors discovery process?
我目前正尝试在 Kotlin 中为 Android 编写注释处理器。项目结构如下:
/annotation
/src/main/kotlin/<package>
Annotation.kt
AnnotationProcessor.kt
/sample
project/build.gradle
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.31"
}
}
annotation/build.gradle
apply plugin: 'kotlin'
sourceCompatibility = 1.7
targetCompatibility = 1.7
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"
}
sample/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"
implementation project(':annotation')
kapt project(':annotation')
}
Annotation.kt
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.SOURCE)
annotation class Annotation(val comment: String = "")
AnnotationProcessor.kt
class AnnotationProcessor : AbstractProcessor() {
override fun process(annotations: MutableSet<out TypeElement>?, roundEnvironment: RoundEnvironment?): Boolean = true
}
使用 kapt,构建在 sample:javaPreCompileDebug 上中断,并显示以下消息:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':sample:javaPreCompileDebug'
Caused by: java.lang.RuntimeException: Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor. Please add them to the annotationProcessor configuration.
- annotation.jar (project :annotation)
Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true to continue with previous behavior. Note that this option is deprecated and will be removed in the future.
See https://developer.android.com/r/tools/annotation-processor-error-message.html for more details.
当我用 annotationProcessor 替换 kapt 时,构建在 sample:transformClassesWithInstantRunForDebug 中断并显示以下消息:
Caused by: java.io.IOException: Failed to find byte code for javax/annotation/processing/AbstractProcessor
at com.android.build.gradle.internal.incremental.AsmUtils.lambda$static[=17=](AsmUtils.java:89)
at com.android.build.gradle.internal.incremental.AsmUtils.loadClass(AsmUtils.java:307)
at com.android.build.gradle.internal.incremental.AsmUtils.readClassAndInterfaces(AsmUtils.java:165)
at com.android.build.gradle.internal.incremental.AsmUtils.loadClass(AsmUtils.java:278)
at com.android.build.gradle.internal.incremental.IncrementalVisitor.instrumentClass(IncrementalVisitor.java:342)
at com.android.build.gradle.internal.transforms.InstantRunTransform.transformToClasses2Format(InstantRunTransform.java:406)
at com.android.build.gradle.internal.transforms.InstantRunTransform.lambda$doTransform(InstantRunTransform.java:268)
at com.android.build.gradle.internal.transforms.InstantRunTransform.lambda$null(InstantRunTransform.java:297)
at java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1424)
... 4 more
当我禁用 Instant 运行 时,一切正常。
我现在的问题是,我的配置哪里出错了?我遵循了像 this 这样的示例项目,我看到的唯一大区别是注释模块分为两个(运行时和编译器)。尽管错误消息表明 Kotlin 或 Instant 运行.
存在问题您必须将 "annotation" 项目分成两部分。
示例:
implementation project(':annotation-lib') // here you put Annotation.kt
kapt project(':annotation-compiler') // AnnotationProcessor.kt
不要忘记将处理器添加到 annotation-compiler 项目中的 META-INF,请参阅 What is the default annotation processors discovery process?