Kotlin 注解处理

Follow these steps:

  1. Apply the kotlin-kapt Gradle plugin:

    KotlinGroovy

    plugins {
        kotlin("kapt") version "1.8.0"
    }
  2. Add the respective dependencies using the kapt configuration in your dependencies block:

    KotlinGroovy

    dependencies {
        kapt("groupId:artifactId:version")
    }
  3. If you previously used the Android support for annotation processors, replace usages of the annotationProcessor configuration with kapt. If your project contains Java classes, kapt will also take care of them.

    If you use annotation processors for your androidTest or test sources, the respective kapt configurations are named kaptAndroidTest and kaptTest. Note that kaptAndroidTest and kaptTest extends kapt, so you can just provide the kapt dependency and it will be available both for production sources and tests.

  4. To use the newest Kotlin features with kapt, for example, repeatable annotations, enable the support for the IR backend with the following option in your gradle.properties:

    kapt.use.jvm.ir=true

Last updated