gpt4 book ai didi

android - 现在必须显式声明 Kapt 注释处理器

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:45 29 4
gpt4 key购买 nike

我正在尝试开发 Kotlin AnnotationProcessor 库,但我不明白为什么会出现此错误:

Error:Execution failed for task ':app:javaPreCompileDebug'.
> 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.
    - compiler.jar (project :compiler)
  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.

我知道,如前所述,我可以使用 includeCompileClasspath = true,我试过了,它工作正常。但如前所述,它已被弃用,很快就会被删除,并且预计将用于您根据 android 文档不使用的库。

所以我正在寻找更清洁的解决方案。

应用模块

你好.kt

@HelloGenerated
class Hello(){
override fun showLog() {
Log.i(Hello::class.simpleName, "${Gahfy_Hello().getName()}")
}
}

构建.gradle

依赖关系{ kapt项目(“:编译器”) compileOnly 项目(“:编译器”) 实现“org.jetbrains.kotlin:kotlin-reflect:$kotlin_version”

编译模块

HelloGenerated.kt

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class HelloGenerated

我也尝试过不使用 Target 和 Retention,但遇到了同样的问题

HelloGenerator.kt

@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"
}
}

build.gradle

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"

resources/META-INF/services/javax.annotation.processing.Processor

net.gahfy.HelloGenerator

我现在正在寻找一个比 includeCompileClasspath = true 更简洁的解决方案。

一些信息:

  • kapt 运行良好,我将它与 Dagger 和 BindingAdapter 一起使用没有任何问题
  • 我的注释处理器在构建时处理得很好,当我设置 includeCompileClasspath = true
  • 时日志中的消息是好的

非常感谢

最佳答案

不确定这是否与您的问题 100% 相关,但在将自动值移动到 kapt 后我遇到了同样的错误。我通过将自动值依赖项声明为 kapt annotationProcessor 来解决它。

所以在你的情况下:

dependencies{
kapt project(":compiler")
annotationProcessor project(":compiler")
compileOnly project(":compiler")
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}

关于android - 现在必须显式声明 Kapt 注释处理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48964912/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com