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

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

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

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

ध्यान दें: Gradle 3.0.0 और इसके बाद के वर्शन के लिए Android प्लग इन, अब 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 और उसके बाद के वर्शन का इस्तेमाल करते समय, आपको प्रोसेसर के उन आर्ग्युमेंट को पास करना होगा जो Gradle के CommandLineArgumentProvider इंटरफ़ेस का इस्तेमाल करके फ़ाइलों या डायरेक्ट्री को दिखाते हैं.

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

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

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

अगर आपके पास कंपाइल क्लासपाथ पर ऐसी डिपेंडेंसी हैं जिनमें ऐसे एनोटेशन प्रोसेसर शामिल हैं जिनकी आपको ज़रूरत नहीं है, तो गड़बड़ी की जांच बंद की जा सकती है. इसके लिए, अपनी 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
       
}
   
}
}

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