অ্যানোটেশন প্রসেসর যোগ করা

এই পৃষ্ঠায় প্রোজেক্ট ডিপেন্ডেন্সি হিসেবে অ্যানোটেশন প্রসেসর যোগ ও কনফিগার করার ব্যাপারে বিস্তারিত নির্দেশিকা দেওয়া আছে। অ্যানোটেশন প্রসেসর সম্পর্কে আরও জানতে, ডিপেন্ডেন্সি কনফিগার করুন বিকল্পে এন্ট্রি দেখুন।

আপনি কম্পাইল ক্লাসপাথে অ্যানোটেশন প্রসেসর যোগ করলে, এই ধরনের ত্রুটি মেসেজ দেখতে পাবেন:

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

গ্ৰুভি

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 এবং তার পরবর্তী যেকোনও ভার্সন ব্যবহার করার সময়, আপনাকে 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 {...}

গ্ৰুভি

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

গ্ৰুভি

// 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 প্রয়োগ করলে কীভাবে বিল্ড পারফর্ম্যান্স উন্নত হয়, সেই সম্পর্কে আরও জানতে, জাভা প্রোজেক্ট ক্যাশে করা নিবন্ধটি পড়ুন।

অ্যানোটেশন প্রসেসর সংক্রান্ত সমস্যা চেক করার সুবিধা বন্ধ করুন

আপনার যদি কম্পাইল ক্লাসপাথের উপর নির্ভরতা থাকে যার মধ্যে অ্যানোটেশন প্রসেসর অন্তর্ভুক্ত থাকে যা আপনার প্রয়োজন নেই, তাহলে আপনি আপনার 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
        }
    }
}

আপনার প্রোজেক্টের অ্যানোটেশন প্রসেসর, প্রসেসর ক্লাসপাথে মাইগ্রেট করার পরে কোনও সমস্যা হলে, আপনি includeCompileClasspath-কে true-এ সেট করে কম্পাইল ক্লাসপাথে অ্যানোটেশন প্রসেসরকে অনুমতি দিতে পারেন। তবে, এই প্রপার্টি true-এ সেট করার সাজেশন দেওয়া হয় না এবং Android প্লাগ-ইনের ভবিষ্যতের আপডেটে এটি করার বিকল্প সরিয়ে দেওয়া হবে।