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

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

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

Error: Annotation processors must be explicitly declared now.

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

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

ดึงดูด

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

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

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

Kotlin

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 และใส่คำอธิบายประกอบอาร์กิวเมนต์แต่ละรายการสำหรับโปรเซสเซอร์

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

ดึงดูด

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

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

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

ดึงดูด

// 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 ในคลาสพาธการคอมไพล์ที่มีโปรเซสเซอร์สำหรับคำอธิบายประกอบซึ่งคุณไม่ต้องการ คุณสามารถปิดใช้การตรวจสอบข้อผิดพลาดได้โดยเพิ่มรายการต่อไปนี้ลงในไฟล์ build.gradle.kts โปรดทราบว่าระบบจะยังคงไม่เพิ่มโปรเซสเซอร์สำหรับคำอธิบายประกอบที่คุณเพิ่มลงในคลาสพาธการคอมไพล์ไปยังคลาสพาธของโปรเซสเซอร์

Kotlin

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

ดึงดูด

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

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

Kotlin

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

ดึงดูด

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

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