Ta strona zawiera szczegółowe wskazówki dotyczące dodawania i konfigurowania procesorów adnotacji jako zależności projektu. Więcej informacji o procesorach adnotacji znajdziesz we wpisie w sekcji Konfigurowanie zależności.
Jeśli dodasz procesory adnotacji do ścieżki klasy kompilacji, zobaczysz komunikat o błędzie podobny do tego:
Error: Annotation processors must be explicitly declared now.
Aby rozwiązać ten problem, dodaj do projektu procesory adnotacji, konfigurując zależność za pomocą annotationProcessor, jak w tym przykładzie:
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") }
Dynamiczny
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' }
Przekazywanie argumentów do procesorów adnotacji
Jeśli musisz przekazać argumenty do procesora adnotacji, możesz to zrobić za pomocą bloku AnnotationProcessorOptions w konfiguracji kompilacji modułu. Jeśli na przykład chcesz przekazywać podstawowe typy danych jako pary klucz-wartość, możesz użyć właściwości argument, jak w tym przykładzie:
Kotlin
android { ... defaultConfig { ... javaCompileOptions { annotationProcessorOptions { arguments += mapOf("key1" to "value1", "key2" to "value2") } } } }
Dynamiczny
android { ... defaultConfig { ... javaCompileOptions { annotationProcessorOptions { argument 'key1', 'value1' argument 'key2', 'value2' } } } }
Jeśli jednak używasz wtyczki Androida do obsługi Gradle w wersji 3.2.0 lub nowszej, musisz przekazać argumenty procesora, które reprezentują pliki lub katalogi, za pomocą interfejsu CommandLineArgumentProvider Gradle.
Użycie CommandLineArgumentProvider pozwala Tobie lub autorowi procesora adnotacji poprawić poprawność i wydajność przyrostowych i buforowanych czystych kompilacji przez zastosowanie adnotacji typu właściwości kompilacji przyrostowej do każdego argumentu.
Na przykład klasa w poniższym przykładzie implementuje interfejs CommandLineArgumentProvider i dodaje adnotacje do każdego argumentu procesora.
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 {...}
Dynamiczny
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 {...}
Po zdefiniowaniu klasy, która implementuje CommandLineArgumentProvider, musisz utworzyć instancję i przekazać ją do wtyczki Androida za pomocą metody annotationProcessorOptions.compilerArgumentProvider, jak pokazano w tym przykładzie.
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"))) } } } }
Dynamiczny
// 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")) } } } }
Aby dowiedzieć się więcej o tym, jak wdrożenie CommandLineArgumentProvider pomaga poprawić wydajność kompilacji, przeczytaj artykuł o buforowaniu projektów Java.
Wyłączanie sprawdzania błędów procesora adnotacji
Jeśli masz zależności w ścieżce klasy kompilacji, które zawierają niepotrzebne procesory adnotacji, możesz wyłączyć sprawdzanie błędów, dodając do pliku build.gradle.kts te informacje. Pamiętaj, że procesory adnotacji dodane do ścieżki klasy kompilacji nadal nie są dodawane do ścieżki klasy procesora.
Kotlin
android { ... defaultConfig { ... javaCompileOptions { annotationProcessorOptions { argument("includeCompileClasspath", "false") } } } }
Dynamiczny
android { ... defaultConfig { ... javaCompileOptions { annotationProcessorOptions { includeCompileClasspath false } } } }
Jeśli używasz języka Kotlin i narzędzia kapt:
Kotlin
android { ... defaultConfig { ... kapt { includeCompileClasspath = false } } }
Dynamiczny
android { ... defaultConfig { ... kapt { includeCompileClasspath false } } }
Jeśli po przeniesieniu procesorów adnotacji projektu do ścieżki klasy procesora wystąpią problemy, możesz zezwolić na procesory adnotacji w ścieżce klasy kompilacji, ustawiając includeCompileClasspath na true. Nie zalecamy jednak ustawiania tej właściwości na true. Opcja ta zostanie usunięta w przyszłej aktualizacji wtyczki Androida.