dependencies{// Adds libraries defining annotations to only the compile classpath.compileOnly("com.google.dagger:dagger:version-number")// Adds the annotation processor dependency to the annotation processor classpath.annotationProcessor("com.google.dagger:dagger-compiler:version-number")}
Groovy
dependencies{// Adds libraries defining annotations to only the compile classpath.compileOnly'com.google.dagger:dagger:version-number'// Adds the annotation processor dependency to the annotation processor classpath.annotationProcessor'com.google.dagger:dagger-compiler:version-number'}
注意:Android Plugin for Gradle 3.0.0 及更高版本不再支持 android-apt 插件。
classMyArgsProvider(// Annotates each directory as either an input or output for the// annotation processor.@get:InputFiles// Using this annotation helps Gradle determine which part of the file path// should be considered during up-to-date checks.@get:PathSensitive(PathSensitivity.RELATIVE)valinputDir:FileCollection,@get:OutputDirectoryvaloutputDir:File):CommandLineArgumentProvider{// Specifies each directory as a command line argument for the processor.// The Android plugin uses this method to pass the arguments to the// annotation processor.overridefunasArguments():Iterable<String>{// Use the form '-Akey[=value]' to pass your options to the Java compiler.returnlistOf("-AinputDir=${inputDir.singleFile.absolutePath}","-AoutputDir=${outputDir.absolutePath}")}}android{...}
Groovy
classMyArgsProviderimplementsCommandLineArgumentProvider{// Annotates each directory as either an input or output for the// annotation processor.@InputFiles// Using this annotation helps Gradle determine which part of the file path// should be considered during up-to-date checks.@PathSensitive(PathSensitivity.RELATIVE)FileCollectioninputDir@OutputDirectoryFileoutputDir// The class constructor sets the paths for the input and output directories.MyArgsProvider(FileCollectioninput,Fileoutput){inputDir=inputoutputDir=output}// Specifies each directory as a command line argument for the processor.// The Android plugin uses this method to pass the arguments to the// annotation processor.@OverrideIterable<String>asArguments(){// Use the form '-Akey[=value]' to pass your options to the Java compiler.["-AinputDir=${inputDir.singleFile.absolutePath}","-AoutputDir=${outputDir.absolutePath}"]}}android{...}
// This is in your module's build.gradle file.android{defaultConfig{javaCompileOptions{annotationProcessorOptions{// Creates a new MyArgsProvider object, specifies the input and// output paths for the constructor, and passes the object// to the Android plugin.compilerArgumentProvider(MyArgsProvider(files("input/path"),file("output/path")))}}}}
Groovy
// This is in your module's build.gradle file.android{defaultConfig{javaCompileOptions{annotationProcessorOptions{// Creates a new MyArgsProvider object, specifies the input and// output paths for the constructor, and passes the object// to the Android plugin.compilerArgumentProvidernewMyArgsProvider(files("input/path"),newFile("output/path"))}}}}
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Add annotation processors\n\nThis page includes detailed guidance on how to add and configure annotation\nprocessors as project dependencies. To learn more about annotation processors,\nsee the entry in\n[Configure dependencies](/build/dependencies#dependency_configurations).\n\nIf you add annotation processors to your compile classpath, you'll see\nan error message similar to the following: \n\n```\nError: Annotation processors must be explicitly declared now.\n```\n\nTo resolve this error, add annotation processors to your project by configuring\nyour dependency using `annotationProcessor` as shown below: \n\n### Kotlin\n\n```kotlin\ndependencies {\n // Adds libraries defining annotations to only the compile classpath.\n compileOnly(\"com.google.dagger:dagger:\u003cvar translate=\"no\"\u003eversion-number\u003c/var\u003e\")\n // Adds the annotation processor dependency to the annotation processor classpath.\n annotationProcessor(\"com.google.dagger:dagger-compiler:\u003cvar translate=\"no\"\u003eversion-number\u003c/var\u003e\")\n}\n```\n\n### Groovy\n\n```groovy\ndependencies {\n // Adds libraries defining annotations to only the compile classpath.\n compileOnly 'com.google.dagger:dagger:\u003cvar translate=\"no\"\u003eversion-number\u003c/var\u003e'\n // Adds the annotation processor dependency to the annotation processor classpath.\n annotationProcessor 'com.google.dagger:dagger-compiler:\u003cvar translate=\"no\"\u003eversion-number\u003c/var\u003e'\n}\n```\n\n**Note:** Android plugin for Gradle 3.0.0+ no longer\nsupports [`android-apt` plugin.](https://github.com/littlerobots/android-apt)\n\nPass arguments to annotation processors\n---------------------------------------\n\nIf you need to pass arguments to an annotation processor, you can do so using\nthe [`AnnotationProcessorOptions`](/reference/tools/gradle-api/current/com/android/build/api/dsl/AnnotationProcessorOptions)\nblock in your module's build configuration. For example, if you want to pass\nprimitive data types as key-value pairs, you can use the `argument` property,\nas shown below: \n\n### Kotlin\n\n```kotlin\nandroid {\n ...\n defaultConfig {\n ...\n javaCompileOptions {\n annotationProcessorOptions {\n arguments += mapOf(\"key1\" to \"value1\",\n \"key2\" to \"value2\")\n }\n }\n }\n}\n```\n\n### Groovy\n\n```groovy\nandroid {\n ...\n defaultConfig {\n ...\n javaCompileOptions {\n annotationProcessorOptions {\n argument 'key1', 'value1'\n argument 'key2', 'value2'\n }\n }\n }\n}\n```\n\nHowever, when using Android Gradle plugin 3.2.0 and higher, you need to\npass processor arguments that represent files or directories using Gradle's\n[`CommandLineArgumentProvider`](https://docs.gradle.org/current/javadoc/org/gradle/process/CommandLineArgumentProvider.html) interface.\n\nUsing `CommandLineArgumentProvider` allows you or the\nannotation processor author to improve the correctness and performance of\nincremental and cached clean builds by applying [incremental build property type\nannotations](https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:up_to_date_checks)\nto each argument.\n\nFor example, the class below implements `CommandLineArgumentProvider` and\nannotates each argument for the processor.\n**Note:** Typically, annotation processor authors provide either this class or instructions on how to write such a class. That's because each argument needs to specify the correct build property type annotation in order to work as intended. \n\n### Kotlin\n\n```kotlin\nclass MyArgsProvider(\n // Annotates each directory as either an input or output for the\n // annotation processor.\n @get:InputFiles\n // Using this annotation helps Gradle determine which part of the file path\n // should be considered during up-to-date checks.\n @get:PathSensitive(PathSensitivity.RELATIVE)\n val inputDir: FileCollection,\n\n @get:OutputDirectory\n val outputDir: File\n) : CommandLineArgumentProvider {\n // Specifies each directory as a command line argument for the processor.\n // The Android plugin uses this method to pass the arguments to the\n // annotation processor.\n\n override fun asArguments(): Iterable\u003cString\u003e {\n // Use the form '-Akey[=value]' to pass your options to the Java compiler.\n return listOf(\"-AinputDir=${inputDir.singleFile.absolutePath}\",\n \"-AoutputDir=${outputDir.absolutePath}\")\n }\n}\n\nandroid {...}\n```\n\n### Groovy\n\n```groovy\nclass MyArgsProvider implements CommandLineArgumentProvider {\n\n // Annotates each directory as either an input or output for the\n // annotation processor.\n @InputFiles\n // Using this annotation helps Gradle determine which part of the file path\n // should be considered during up-to-date checks.\n @PathSensitive(PathSensitivity.RELATIVE)\n FileCollection inputDir\n\n @OutputDirectory\n File outputDir\n\n // The class constructor sets the paths for the input and output directories.\n MyArgsProvider(FileCollection input, File output) {\n inputDir = input\n outputDir = output\n }\n\n // Specifies each directory as a command line argument for the processor.\n // The Android plugin uses this method to pass the arguments to the\n // annotation processor.\n @Override\n Iterable\u003cString\u003e asArguments() {\n // Use the form '-Akey[=value]' to pass your options to the Java compiler.\n [\"-AinputDir=${inputDir.singleFile.absolutePath}\",\n \"-AoutputDir=${outputDir.absolutePath}\"]\n }\n}\n\nandroid {...}\n```\n\nAfter you define a class that implements `CommandLineArgumentProvider`, you need\nto create an instance and pass it to the Android plugin using the\n[`annotationProcessorOptions.compilerArgumentProvider`](/reference/tools/gradle-api/current/com/android/build/api/dsl/AnnotationProcessorOptions#compilerArgumentProvider(org.gradle.process.CommandLineArgumentProvider))\nmethod, as shown below. \n\n### Kotlin\n\n```kotlin\n// This is in your module's build.gradle file.\nandroid {\n defaultConfig {\n javaCompileOptions {\n annotationProcessorOptions {\n // Creates a new MyArgsProvider object, specifies the input and\n // output paths for the constructor, and passes the object\n // to the Android plugin.\n compilerArgumentProvider(MyArgsProvider(files(\"input/path\"),\n file(\"output/path\")))\n }\n }\n }\n}\n```\n\n### Groovy\n\n```groovy\n// This is in your module's build.gradle file.\nandroid {\n defaultConfig {\n javaCompileOptions {\n annotationProcessorOptions {\n // Creates a new MyArgsProvider object, specifies the input and\n // output paths for the constructor, and passes the object\n // to the Android plugin.\n compilerArgumentProvider new MyArgsProvider(files(\"input/path\"),\n new File(\"output/path\"))\n }\n }\n }\n}\n```\n\nTo learn more about how implementing `CommandLineArgumentProvider` helps\nimprove build performance, read\n[Caching Java projects](https://docs.gradle.org/current/userguide/build_cache_use_cases.html).\n\nDisable the annotation processor error check\n--------------------------------------------\n\nIf you have dependencies on the compile classpath that include annotation\nprocessors you don't need, you can disable the error check by adding\nthe following to your `build.gradle.kts` file. Keep in mind, the annotation\nprocessors you add to the compile classpath are still not added to the\nprocessor classpath. \n\n### Kotlin\n\n```kotlin\nandroid {\n ...\n defaultConfig {\n ...\n javaCompileOptions {\n annotationProcessorOptions {\n argument(\"includeCompileClasspath\", \"false\")\n }\n }\n }\n}\n```\n\n### Groovy\n\n```groovy\nandroid {\n ...\n defaultConfig {\n ...\n javaCompileOptions {\n annotationProcessorOptions {\n includeCompileClasspath false\n }\n }\n }\n}\n```\n\nIf you use Kotlin and [kapt](https://kotlinlang.org/docs/kapt.html): \n\n### Kotlin\n\n```kotlin\nandroid {\n ...\n defaultConfig {\n ...\n kapt {\n includeCompileClasspath = false\n }\n }\n}\n```\n\n### Groovy\n\n```groovy\nandroid {\n ...\n defaultConfig {\n ...\n kapt {\n includeCompileClasspath false\n }\n }\n}\n```\n\nIf you experience issues after migrating your project's annotation processors to\nthe processor classpath, you can allow annotation processors on the compile\nclasspath by setting `includeCompileClasspath` to `true`. However, setting this\nproperty to `true` is not recommended, and the option to do so will be removed\nin a future update of the Android plugin."]]