Diese Seite enthält eine detaillierte Anleitung zum Hinzufügen und Konfigurieren von Annotationsprozessoren als Projektabhängigkeiten. Weitere Informationen zu Annotationsprozessoren finden Sie im Eintrag unter Abhängigkeiten konfigurieren.
Wenn Sie Ihrem Kompilierungs-Klassenpfad Annotationsprozessoren hinzufügen, wird eine Fehlermeldung ähnlich der folgenden angezeigt:
Error: Annotation processors must be explicitly declared now.
Fügen Sie Ihrem Projekt Annotationsprozessoren hinzu, um diesen Fehler zu beheben. Konfigurieren Sie dazu die Abhängigkeit mit annotationProcessor, wie im folgenden Beispiel gezeigt:
Kotlin
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' }
Argumente an Annotationsprozessoren übergeben
Wenn Sie Argumente an einen Annotationsprozessor übergeben müssen, können Sie dazu den Block AnnotationProcessorOptions in der Build-Konfiguration Ihres Moduls verwenden. Wenn Sie beispielsweise primitive Datentypen als Schlüssel/Wert-Paare übergeben möchten, können Sie das Attribut argument verwenden, wie im folgenden Beispiel gezeigt:
Kotlin
android { ... defaultConfig { ... javaCompileOptions { annotationProcessorOptions { arguments += mapOf("key1" to "value1", "key2" to "value2") } } } }
Groovy
android { ... defaultConfig { ... javaCompileOptions { annotationProcessorOptions { argument 'key1', 'value1' argument 'key2', 'value2' } } } }
Wenn Sie jedoch das Android-Gradle-Plug-in 3.2.0 oder höher verwenden, müssen Sie Prozessorargumente, die Dateien oder Verzeichnisse darstellen, über die CommandLineArgumentProvider-Schnittstelle von Gradle übergeben.
Mit CommandLineArgumentProvider können Sie oder der Autor des Annotationsprozessors die Korrektheit und Leistung von inkrementellen und im Cache gespeicherten Clean Builds verbessern, indem Sie auf jedes Argument Annotationen für inkrementelle Build-Attributtypen anwenden.
Die Klasse im folgenden Beispiel implementiert beispielsweise CommandLineArgumentProvider und annotiert jedes Argument für den Prozessor.
Kotlin
class MyArgsProvider( // 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 path // to consider during up-to-date checks. @get:PathSensitive(PathSensitivity.RELATIVE) val inputDir: FileCollection, @get:OutputDirectory val outputDir: 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. override fun asArguments(): Iterable<String> { // Use the form '-Akey[=value]' to pass your options to the Java compiler. return listOf("-AinputDir=${inputDir.singleFile.absolutePath}", "-AoutputDir=${outputDir.absolutePath}") } } android {...}
Groovy
class MyArgsProvider implements CommandLineArgumentProvider { // Annotates each directory as either an input or output for the // annotation processor. @InputFiles // Using this annotation helps Gradle determine which part of the path // to consider during up-to-date checks. @PathSensitive(PathSensitivity.RELATIVE) FileCollection inputDir @OutputDirectory File outputDir // The class constructor sets the paths for the input and output directories. MyArgsProvider(FileCollection input, File output) { inputDir = input outputDir = 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. @Override Iterable<String> asArguments() { // Use the form '-Akey[=value]' to pass your options to the Java compiler. ["-AinputDir=${inputDir.singleFile.absolutePath}", "-AoutputDir=${outputDir.absolutePath}"] } } android {...}
Nachdem Sie eine Klasse definiert haben, die CommandLineArgumentProvider implementiert, müssen Sie eine Instanz erstellen und sie mit der Methode annotationProcessorOptions.compilerArgumentProvider an das Android-Plug-in übergeben, wie im folgenden Beispiel gezeigt.
Kotlin
// 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. compilerArgumentProvider new MyArgsProvider(files("input/path"), new File("output/path")) } } } }
Weitere Informationen dazu, wie die Implementierung von CommandLineArgumentProvider die Build-Leistung verbessert, finden Sie unter Java-Projekte im Cache speichern.
Fehlerprüfung des Annotationsprozessors deaktivieren
Wenn Sie Abhängigkeiten im Kompilierungs-Klassenpfad haben, die nicht benötigte Annotationsprozessoren enthalten, können Sie die Fehlerprüfung deaktivieren, indem Sie Folgendes in Ihre build.gradle.kts-Datei einfügen. Beachten Sie, dass die Annotationsprozessoren, die Sie dem Kompilierungs-Klassenpfad hinzufügen, nicht dem Prozessor-Klassenpfad hinzugefügt werden.
Kotlin
android { ... defaultConfig { ... javaCompileOptions { annotationProcessorOptions { argument("includeCompileClasspath", "false") } } } }
Groovy
android { ... defaultConfig { ... javaCompileOptions { annotationProcessorOptions { includeCompileClasspath false } } } }
Wenn Sie Kotlin und kapt verwenden:
Kotlin
android { ... defaultConfig { ... kapt { includeCompileClasspath = false } } }
Groovy
android { ... defaultConfig { ... kapt { includeCompileClasspath false } } }
Wenn nach der Migration der Annotationsprozessoren Ihres Projekts zum Prozessor-Klassenpfad Probleme auftreten, können Sie Annotationsprozessoren im Kompilierungs-Klassenpfad zulassen, indem Sie includeCompileClasspath auf true setzen. Es wird jedoch nicht empfohlen, dieses Attribut auf true festzulegen. Die Option wird in einem zukünftigen Update des Android-Plug-ins entfernt.