Bu sayfada, ek açıklama işlemcilerinin proje bağımlılıkları olarak nasıl ekleneceği ve yapılandırılacağı hakkında ayrıntılı yönergeler bulunmaktadır. Ek açıklama işlemcileri hakkında daha fazla bilgi edinmek için Bağımlılıkları yapılandırma girişine bakın.
Derleme sınıf yolunuza ek açıklama işlemcileri eklerseniz aşağıdakine benzer bir hata mesajı görürsünüz:
Error: Annotation processors must be explicitly declared now.
Bu hatayı çözmek için annotationProcessor
ile bağımlılığınızı aşağıdaki gibi yapılandırarak projenize ek açıklama işlemcileri 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' }
Not: Gradle 3.0.0+ için Android eklentisi artık
android-apt
eklentisini desteklememektedir.
Bağımsız değişkenleri ek açıklama işlemcilerine aktarma
Bağımsız değişkenleri bir ek açıklama işlemcisine aktarmanız gerekiyorsa bunu modülünüzün derleme yapılandırmasındaki AnnotationProcessorOptions
bloğunu kullanarak yapabilirsiniz. Örneğin, temel veri türlerini anahtar/değer çiftleri olarak iletmek isterseniz aşağıda 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, dosya veya dizinleri temsil eden işlemci bağımsız değişkenlerini Gradle'ın CommandLineArgumentProvider
arayüzünü kullanarak iletmeniz gerekir.
CommandLineArgumentProvider
kullanıldığında, her bağımsız değişkene artımlı derleme özelliği türü ek açıklamaları uygulanarak artımlı ve önbelleğe alınmış temiz derlemelerin doğruluğu ve performansını iyileştirebilirsiniz. Bu sayede, ek açıklama işleyicisi de geliştirebilirsiniz.
Örneğin, aşağıdaki sınıf CommandLineArgumentProvider
işlemini uygular ve işlemci için her bağımsız değişkene ek açıklama 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, aşağıda gösterildiği gibi bir örnek oluşturmanız ve 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ı uygulamanın derleme performansını iyileştirmeye nasıl yardımcı olduğu hakkında daha fazla bilgi edinmek için Java projelerini önbelleğe alma bölümünü okuyun.
Ek açıklama işlemcisi hata kontrolünü devre dışı bırak
İhtiyacınız olmayan ek açıklama işlemcilerini içeren derleme sınıf yolunda bağımlılıklarınız varsa aşağıdakini build.gradle.kts
dosyanıza ekleyerek hata kontrolünü devre dışı bırakabilirsiniz. Derleme sınıf yoluna eklediğiniz ek açıklama işlemcilerinin yine de işlemci 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şlemcilerini işlemci 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şlemcilerine izin verebilirsiniz. Ancak, bu özelliğin true
olarak ayarlanması önerilmez ve ayarlama seçeneği Android eklentisinin gelecekteki bir güncellemesinde kaldırılacaktır.