เพิ่มตัวประมวลผลคำอธิบายประกอบ

หน้านี้มีคําแนะนําโดยละเอียดเกี่ยวกับวิธีเพิ่มและกําหนดค่าโปรแกรมประมวลผลคําอธิบายประกอบเป็นข้อกําหนดของโปรเจ็กต์ ดูข้อมูลเพิ่มเติมเกี่ยวกับโปรแกรมประมวลผลคำอธิบายประกอบได้ที่รายการในกำหนดค่า Dependency

หากเพิ่มตัวประมวลผลคำอธิบายประกอบลงในเส้นทางคอมไพล์ คุณจะเห็นข้อความแสดงข้อผิดพลาดที่คล้ายกับข้อความต่อไปนี้

Error: Annotation processors must be explicitly declared now.

หากต้องการแก้ไขข้อผิดพลาดนี้ ให้เพิ่มโปรแกรมประมวลผลคำอธิบายประกอบลงในโปรเจ็กต์โดยกำหนดค่าข้อกำหนดโดยใช้ annotationProcessor ดังที่แสดงด้านล่าง

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

หมายเหตุ: ปลั๊กอิน Android สำหรับ Gradle 3.0.0 ขึ้นไปไม่รองรับปลั๊กอิน android-apt อีกต่อไป

ส่งอาร์กิวเมนต์ไปยังโปรแกรมประมวลผลคำอธิบายประกอบ

หากต้องการส่งอาร์กิวเมนต์ไปยังโปรแกรมประมวลผลคำอธิบายประกอบ ให้ใช้บล็อก AnnotationProcessorOptions ในการกำหนดค่าบิลด์ของโมดูล ตัวอย่างเช่น หากต้องการส่งประเภทข้อมูลพื้นฐานเป็นคู่คีย์-ค่า ให้ใช้พร็อพเพอร์ตี้ argument ดังที่แสดงด้านล่าง

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

อย่างไรก็ตาม เมื่อใช้ปลั๊กอิน Android Gradle 3.2.0 ขึ้นไป คุณต้องส่งอาร์กิวเมนต์ตัวประมวลผลที่แสดงไฟล์หรือไดเรกทอรีโดยใช้อินเทอร์เฟซ CommandLineArgumentProvider ของ Gradle

การใช้ CommandLineArgumentProvider จะช่วยให้คุณหรือผู้เขียนโปรแกรมประมวลผลคำอธิบายประกอบปรับปรุงความถูกต้องและประสิทธิภาพของการสร้างที่สะอาดแบบเพิ่มและแบบแคชได้โดยใช้คำอธิบายประกอบประเภทพร็อพเพอร์ตี้การสร้างแบบเพิ่มกับอาร์กิวเมนต์แต่ละรายการ

ตัวอย่างเช่น คลาสด้านล่างใช้ CommandLineArgumentProvider และอธิบายประกอบอาร์กิวเมนต์แต่ละรายการสําหรับตัวประมวลผล

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

หลังจากกำหนดคลาสที่ใช้ CommandLineArgumentProvider แล้ว คุณจะต้องสร้างอินสแตนซ์และส่งไปยังปลั๊กอิน Android โดยใช้เมธอด annotationProcessorOptions.compilerArgumentProvider ดังที่แสดงด้านล่าง

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

ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีที่การใช้ CommandLineArgumentProvider ช่วยปรับปรุงประสิทธิภาพการสร้างได้จากบทความการแคชโปรเจ็กต์ Java

ปิดใช้การตรวจสอบข้อผิดพลาดของโปรแกรมประมวลผลคำอธิบายประกอบ

หากมี Dependency ใน Compile Classpath ที่มีตัวประมวลผลคำอธิบายประกอบที่คุณไม่ต้องการ คุณปิดใช้การตรวจสอบข้อผิดพลาดได้โดยเพิ่มข้อมูลต่อไปนี้ลงในไฟล์ build.gradle.kts โปรดทราบว่าตัวประมวลผลคำอธิบายประกอบที่คุณเพิ่มลงในเส้นทางคอมไพล์จะยังไม่เพิ่มลงในเส้นทางคอมไพล์ของตัวประมวลผล

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

หากคุณใช้ Kotlin และ kapt ให้ทำดังนี้

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

หากพบปัญหาหลังจากย้ายข้อมูลโปรแกรมประมวลผลคำอธิบายประกอบของโปรเจ็กต์ไปยัง classpath ของโปรแกรมประมวลผล คุณสามารถอนุญาตให้โปรแกรมประมวลผลคำอธิบายประกอบใน classpath ของคอมไพล์ได้โดยตั้งค่า includeCompileClasspath เป็น true อย่างไรก็ตาม เราขอแนะนำไม่ให้ตั้งค่าพร็อพเพอร์ตี้นี้เป็น true และตัวเลือกในการตั้งค่าดังกล่าวจะถูกนำออกในการอัปเดตปลั๊กอิน Android ในอนาคต