Android 现在必须显式声明Kapt注释处理器
我正在尝试开发Kotlin AnnotationProcessor库,但我不明白为什么会出现此错误: 错误:任务“:app:javaPreCompileDebug”的执行失败。Android 现在必须显式声明Kapt注释处理器,android,gradle,kotlin,annotation-processor,Android,Gradle,Kotlin,Annotation Processor,我正在尝试开发Kotlin AnnotationProcessor库,但我不明白为什么会出现此错误: 错误:任务“:app:javaPreCompileDebug”的执行失败。 >现在必须显式声明注释处理器。发现编译类路径上的以下依赖项包含注释处理器。请将它们添加到annotationProcessor配置中。 -compiler.jar(项目:compiler) 或者,设置android.defaultConfig.javaCompileOptions.annotationProcessorO
>现在必须显式声明注释处理器。发现编译类路径上的以下依赖项包含注释处理器。请将它们添加到annotationProcessor配置中。
-compiler.jar(项目:compiler)
或者,设置android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath=true以继续前面的行为。请注意,此选项已弃用,将来将被删除。
有关更多详细信息,请参阅 我知道正如前面提到的,我可以使用
includeCompileClasspath=true
,我试过了,效果很好。但正如前面提到的,它已经被弃用,很快就被删除了,而且根据android文档,它预计将用于您不使用的库
所以我在寻找一种更清洁的解决方案
应用程序模块
你好,kt
格雷德尔先生
依赖关系{
kapt项目(“编译器”)
compileOnly项目(“编译器”)
实现“org.jetbrains.kotlin:kotlin reflect:$kotlin_version”
}
编译模块
希腊化
我也尝试过没有目标和保留,也有同样的问题
HelloGenerator.kt
参考资料/META-INF/services/javax.annotation.processing.Processor
我现在正在寻找一个更干净的解决方案,而不仅仅是includeCompileClasspath=true
一些信息:
- kapt工作得很好,我使用Dagger和BindingAdapter时没有任何问题
- 我的注释处理器在构建时处理得很好,当我设置
includeCompileClasspath=true时,日志中的消息是好消息
非常感谢我不确定这是否与您的问题100%相关,但在将自动值移动到
kapt
后,我出现了相同的错误。我通过将自动值依赖项声明为kapt
和
解决了这个问题
因此,在你的情况下:
dependencies{
kapt project(":compiler")
annotationProcessor project(":compiler")
compileOnly project(":compiler")
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}
看起来你也在jetbrains回购协议中对这个问题发表了评论:它对我也适用,但它似乎是AS 3或Gradle中一个bug的解决方法?确实如此。还没有找到更好的解决方案,不幸的是,我已经在Android bug tracker中记录了一个bug:
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class HelloGenerated
@SupportedAnnotationTypes("net.gahfy.HelloGenerated")
class HelloGeneratedInjectorProcessor: AbstractProcessor() {
override fun getSupportedAnnotationTypes(): MutableSet<String> {
return mutableSetOf(HelloGenerated::class.java.name)
}
override fun getSupportedSourceVersion(): SourceVersion {
return SourceVersion.latest()
}
override fun process(annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean {
val annotation = annotations.firstOrNull { it.toString() == "net.gahfy.HelloGenerated" } ?: return false
for (element in roundEnv.getElementsAnnotatedWith(annotation)) {
val className = element.simpleName.toString()
val `package` = processingEnv.elementUtils.getPackageOf(element).toString()
generateClass(className, `package`)
}
return true
}
private fun generateClass(className: String, `package`: String) {
val kotlinGeneratedPath = (processingEnv.options["kapt.kotlin.generated"] as String).replace("kaptKotlin", "kapt")
val kaptKotlinGenerated = File(kotlinGeneratedPath)
val source = "package $`package`\n\nclass Lachazette_$className(){fun getName():String{return \"World\"}}"
val relativePath = `package`.replace('.', File.separatorChar)
val folder = File(kaptKotlinGenerated, relativePath).apply { if(!exists()){mkdirs()} }
File(folder, "Lachazette_$className.kt").writeText(source)
}
companion object {
const val KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated"
}
}
apply plugin: 'java-library'
apply plugin: 'kotlin'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
net.gahfy.HelloGenerator
dependencies{
kapt project(":compiler")
annotationProcessor project(":compiler")
compileOnly project(":compiler")
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}