Kapt注解处理器现在必须明确声明

4
我正在尝试开发一个Kotlin注解处理器库,但是我无法弄清楚为什么会出现以下错误:
执行任务': app:javaPreCompileDebug'失败。 > 现在必须明确声明注释处理器。 在编译类路径上找到以下依赖项包含注释处理器。 请将它们添加到annotationProcessor配置中。     - compiler.jar(项目:compiler)   或者,设置android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true以继续使用以前的行为。 请注意,此选项已过时,并将在未来删除。   有关详细信息,请参见https://developer.android.com/r/tools/annotation-processor-error-message.html
我知道如上所述,我可以使用includeCompileClasspath = true并尝试了一下,它运行良好。但是,如上所述,它已被弃用,即将删除,并且根据android文档,预计将用于您不使用的库。
因此,我正在寻找更干净的解决方案。

应用程序模块

Hello.kt

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

Build.gradle

依赖项{ kapt 项目(":compiler") compileOnly 项目(":compiler") implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" }

编译器模块

HelloGenerated.kt

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

我也尝试过不使用目标和保留,但仍然出现了相同的问题。

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时,我的注释处理器被很好地处理,并且日志中的消息是正确的。
非常感谢。
1个回答

1

我不确定这是否与你的问题完全相关,但在将 auto-value 移动到 kapt 后,我遇到了相同的错误。我通过将 auto-value 依赖项声明为 kaptannotationProcessor 来解决了它。

所以在你的情况下:

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

看起来你也在JetBrains存储库的问题上发表了评论:https://github.com/JetBrains/kotlin-examples/issues/59 这对我也有效,但似乎是AS 3或Gradle中的错误解决方法? - Maks
确实。不幸的是,我还没有找到更好的解决方案。 - Fabian Streitel
1
我已经在Android错误跟踪器中记录了这个bug:https://issuetracker.google.com/issues/80270236 - Maks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接