जानकारी देने वाले प्रोसेसर जोड़ें

इस पेज पर, प्रोजेक्ट डिपेंडेंसी के तौर पर व्याख्या प्रोसेसर जोड़ने और उन्हें कॉन्फ़िगर करने के बारे में ज़्यादा जानकारी दी गई है. व्याख्या प्रोसेसर के बारे में ज़्यादा जानने के लिए, डिपेंडेंसी कॉन्फ़िगर करें में दी गई जानकारी देखें.

अगर आपने कंपाइल क्लासपाथ में व्याख्या प्रोसेसर जोड़े हैं, तो आपको गड़बड़ी का यह मैसेज दिखेगा:

Error: Annotation processors must be explicitly declared now.

इस गड़बड़ी को ठीक करने के लिए, अपने प्रोजेक्ट में व्याख्या प्रोसेसर जोड़ें. इसके लिए, 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")
}

Groovy

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

Groovy

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

हालांकि, Android Gradle प्लगिन 3.2.0 और इसके बाद के वर्शन का इस्तेमाल करते समय, आपको Gradle के CommandLineArgumentProvider इंटरफ़ेस का इस्तेमाल करके, फ़ाइलों या डायरेक्ट्री को दिखाने वाले प्रोसेसर आर्ग्युमेंट पास करने होंगे.

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

Groovy

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 को लागू करने वाली क्लास तय करने के बाद, आपको एक इंस्टेंस बनाना होगा. इसके बाद, annotationProcessorOptions.compilerArgumentProvider तरीके का इस्तेमाल करके, इसे Android प्लगिन को पास करना होगा. इसके बारे में यहां दिए गए उदाहरण में बताया गया है.

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

Groovy

// 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 प्रोजेक्ट को कैश मेमोरी में सेव करना लेख पढ़ें.

व्याख्या प्रोसेसर की गड़बड़ी की जांच करने की सुविधा बंद करना

अगर आपके पास कंपाइल क्लासपाथ पर ऐसी डिपेंडेंसी हैं जिनमें व्याख्या प्रोसेसर शामिल हैं और आपको उनकी ज़रूरत नहीं है, तो अपनी build.gradle.kts फ़ाइल में यह जोड़कर गड़बड़ी की जांच को बंद किया जा सकता है. ध्यान रखें कि कंपाइल क्लासपाथ में जोड़े गए व्याख्या प्रोसेसर, अब भी प्रोसेसर क्लासपाथ में नहीं जोड़े जाते.

Kotlin

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

Groovy

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

अगर Kotlin और kapt का इस्तेमाल किया जाता है, तो:

Kotlin

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

Groovy

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

अगर अपने प्रोजेक्ट के व्याख्या प्रोसेसर को प्रोसेसर क्लासपाथ पर माइग्रेट करने के बाद आपको समस्याएं आती हैं, तो कंपाइल क्लासपाथ पर व्याख्या प्रोसेसर को अनुमति दी जा सकती है. इसके लिए, includeCompileClasspath को true पर सेट करें. हालांकि, इस प्रॉपर्टी को true पर सेट करने का सुझाव नहीं दिया जाता है. साथ ही, Android प्लगिन के आने वाले अपडेट में, ऐसा करने का विकल्प हटा दिया जाएगा.