【问题标题】:Kotlin annotation processor: can't make it workKotlin 注释处理器:无法使其工作
【发布时间】:2017-09-04 07:37:26
【问题描述】:

我的 gradle 构建:

buildscript {
    ext.kotlin_version = '1.1.4-3'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: "kotlin-kapt"

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


kapt {
    processors = "libs.orm.codeGenerators.ModelProcessor" //PROCESSOR
}


dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"

    compile "com.google.auto.service:auto-service:1.0-rc3"   
}

处理器不在单独的模块中。

处理器什么都不做,在#process 它只是抛出,看看它是否在工作。

@AutoService(Processor::class)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
class ModelProcessor : AbstractProcessor() {

    override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment): Boolean {
        throw(Throwable("foo"))
        return true
    }

    override fun getSupportedAnnotationTypes() : MutableSet<String> {
        return mutableSetOf<String>("*")
    }

}

但绝对没有任何反应。没有错误,什么都没有。 我怎样才能让它发挥作用?

【问题讨论】:

  • 要使其工作,您需要将处理器作为依赖项添加到kapt 配置中,这是 kapt 搜索处理器的地方。你可以通过分离模块然后dependencies { kapt project(':processor') }来做到这一点。
  • 移至处理器和应用程序的单独模块。现在在构建时:Sources output directory is not specified for processor_main, skipping annotation processing
  • @Sheppard 你成功了吗?我仍然遇到@AutoService 的问题
  • 不,我只得到了它在接受的答案中的工作方式(通过配置 META-INF)。错误Sources output directory is not specified for processor_main, skipping annotation processing 仍然被记录,但生成的代码忽略它。

标签: kotlin kapt


【解决方案1】:

在我的实践中,AutoService 只是忽略 kotlin 类。您必须改用 java 类,或者编写自己的 META-INF:

main/resources/META-INF/services/javax.annotation.processing.Processor 并包含:your.package.ModelProcessor

【讨论】:

    猜你喜欢
    • 2018-09-21
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 2017-05-22
    相关资源
    最近更新 更多