Anmerkungsprozessoren hinzufügen

Auf dieser Seite finden Sie eine detaillierte Anleitung zum Hinzufügen und Konfigurieren von Anmerkungs-Prozessoren als Projektabhängigkeiten. Weitere Informationen zu Anmerkungs-Prozessoren finden Sie unter Abhängigkeiten konfigurieren.

Wenn Sie Ihrem Compile-Classpath Anmerkungs-Prozessoren hinzufügen, wird eine Fehlermeldung ähnlich der folgenden angezeigt:

Error: Annotation processors must be explicitly declared now.

Um diesen Fehler zu beheben, fügen Sie Ihrem Projekt Anmerkungs-Prozessoren hinzu. Konfigurieren Sie dazu Ihre Abhängigkeit wie unten gezeigt mit annotationProcessor:

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")
}
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'
}

Hinweis:Das Android-Plug-in für Gradle 3.0.0 und höher unterstützt das android-apt-Plug-in nicht mehr.

Argumente an Annotation Processors übergeben

Wenn Sie einem Anmerkungs-Prozessor Argumente übergeben möchten, können Sie dies mithilfe des Blocks AnnotationProcessorOptions in der Build-Konfiguration Ihres Moduls tun. Wenn Sie beispielsweise primitive Datentypen als Schlüssel/Wert-Paare übergeben möchten, können Sie die Property argument verwenden, wie unten gezeigt:

android {
   
...
    defaultConfig
{
       
...
        javaCompileOptions
{
            annotationProcessorOptions
{
                arguments
+= mapOf("key1" to "value1",
                                   
"key2" to "value2")
           
}
       
}
   
}
}
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 Gradle-Schnittstelle CommandLineArgumentProvider übergeben.

Mit CommandLineArgumentProvider können Sie oder der Autor des Anmerkungs-Prozessors die Korrektheit und Leistung von inkrementellen und im Cache gespeicherten sauberen Builds verbessern, indem Sie auf jedes Argument Anmerkungen zum Inkrements-Build-Eigenschaftstyp anwenden.

In der folgenden Klasse wird beispielsweise CommandLineArgumentProvider implementiert und jedes Argument für den Prozessor mit Anmerkungen versehen.

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 file path
   
// should be considered 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
{...}
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 file path
   
// should be considered 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 wie unten gezeigt mit der Methode annotationProcessorOptions.compilerArgumentProvider an das Android-Plug-in übergeben.

// 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")))
           
}
       
}
   
}
}
// 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 Sie mit CommandLineArgumentProvider die Build-Leistung verbessern, finden Sie unter Java-Projekte im Cache speichern.

Fehlerprüfung des Annotations-Prozessors deaktivieren

Wenn Sie Abhängigkeiten im Compile-Klassenpfad haben, die Annotationsprozessoren enthalten, die Sie nicht benötigen, können Sie die Fehlerprüfung deaktivieren, indem Sie der Datei build.gradle.kts Folgendes hinzufügen: Beachten Sie, dass die Anmerkungs-Prozessoren, die Sie dem Compile-ClassPath hinzufügen, nicht dem Processor-ClassPath hinzugefügt werden.

android {
   
...
    defaultConfig
{
       
...
        javaCompileOptions
{
            annotationProcessorOptions
{
                argument
("includeCompileClasspath", "false")
           
}
       
}
   
}
}
android {
   
...
    defaultConfig
{
       
...
        javaCompileOptions
{
            annotationProcessorOptions
{
                includeCompileClasspath
false
           
}
       
}
   
}
}

Wenn Sie Kotlin und kapt verwenden:

android {
   
...
    defaultConfig
{
       
...
        kapt
{
            includeCompileClasspath
= false
       
}
   
}
}
android {
   
...
    defaultConfig
{
       
...
        kapt
{
            includeCompileClasspath
false
       
}
   
}
}

Wenn nach der Migration der Annotations-Prozessoren Ihres Projekts in den Prozessor-Classpath Probleme auftreten, können Sie Annotations-Prozessoren im Compile-Classpath zulassen, indem Sie includeCompileClasspath auf true festlegen. Wir empfehlen jedoch, diese Eigenschaft nicht auf true festzulegen. Diese Option wird in einem zukünftigen Update des Android-Plug-ins entfernt.