Ek açıklama işlemcileri ekleme

Bu sayfada, ek açıklama işleyicileri proje bağımlılıkları olarak ekleme ve yapılandırma hakkında ayrıntılı bilgiler yer alır. Ek açıklama işleyiciler hakkında daha fazla bilgi edinmek için Bağımlılıkları yapılandırma bölümündeki girişe bakın.

Derleme sınıf yolunuza ek açıklama işleyicileri eklerseniz aşağıdakine benzer bir hata mesajı görürsünüz:

Error: Annotation processors must be explicitly declared now.

Bu hatayı düzeltmek için aşağıdaki örnekte gösterildiği gibi annotationProcessor kullanarak bağımlılığınızı yapılandırarak projenize ek açıklama işleyicileri ekleyin:

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

Modern

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

Bağımsız değişkenleri ek açıklama işleyicilerine iletme

Bir ek açıklama işleyicisine bağımsız değişkenler iletmeniz gerekiyorsa modülünüzün derleme yapılandırmasındaki AnnotationProcessorOptions bloğunu kullanarak bunu yapabilirsiniz. Örneğin, temel veri türlerini anahtar/değer çiftleri olarak iletmek istiyorsanız aşağıdaki örnekte gösterildiği gibi argument özelliğini kullanabilirsiniz:

Kotlin

android {
    ...
    defaultConfig {
        ...
        javaCompileOptions {
            annotationProcessorOptions {
                arguments += mapOf("key1" to "value1",
                                   "key2" to "value2")
            }
        }
    }
}

Modern

android {
    ...
    defaultConfig {
        ...
        javaCompileOptions {
            annotationProcessorOptions {
                argument 'key1', 'value1'
                argument 'key2', 'value2'
            }
        }
    }
}

Ancak Android Gradle eklentisi 3.2.0 ve sonraki sürümleri kullanırken Gradle'ın CommandLineArgumentProvider arayüzünü kullanarak dosyaları veya dizinleri temsil eden işlemci bağımsız değişkenlerini iletmeniz gerekir.

CommandLineArgumentProvider kullanmak, her bağımsız değişkene artımlı derleme özellik türü ek açıklamaları uygulayarak sizin veya ek açıklama işleyicisi yazarının artımlı ve önbelleğe alınmış temiz derlemelerin doğruluğunu ve performansını artırmanıza olanak tanır.

Örneğin, aşağıdaki örnekteki sınıf CommandLineArgumentProvider öğesini uygular ve işlemci için her bağımsız değişkeni ekler.

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 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 {...}

Modern

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 {...}

CommandLineArgumentProvider uygulayan bir sınıf tanımladıktan sonra bir örnek oluşturmanız ve aşağıdaki örnekte gösterildiği gibi annotationProcessorOptions.compilerArgumentProvider yöntemini kullanarak bu örneği Android eklentisine iletmeniz gerekir.

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

Modern

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

CommandLineArgumentProvider uygulamasının derleme performansını artırmaya nasıl yardımcı olduğu hakkında daha fazla bilgi edinmek için Java projelerini önbelleğe alma başlıklı makaleyi okuyun.

Ek açıklama işleyicisi hata kontrolünü devre dışı bırakma

Derleme sınıf yolunda, ihtiyacınız olmayan ek açıklama işleyicileri içeren bağımlılıklarınız varsa build.gradle.kts dosyanıza aşağıdakileri ekleyerek hata kontrolünü devre dışı bırakabilirsiniz. Derleme sınıf yoluna eklediğiniz ek açıklama işleyicilerin, işleyici sınıf yoluna eklenmediğini unutmayın.

Kotlin

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

Modern

android {
    ...
    defaultConfig {
        ...
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath false
            }
        }
    }
}

Kotlin ve kapt kullanıyorsanız:

Kotlin

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

Modern

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

Projenizin ek açıklama işleyicilerini işleyici sınıf yoluna taşıdıktan sonra sorun yaşarsanız includeCompileClasspath değerini true olarak ayarlayarak derleme sınıf yolunda ek açıklama işleyicilerine izin verebilirsiniz. Ancak bu özelliğin true olarak ayarlanması önerilmez ve bu seçenek, Android eklentisinin gelecekteki bir güncellemesinde kaldırılacaktır.