গুগল সার্ভিসেস গ্রেডল প্লাগইন

ভূমিকা

As part of enabling Google APIs or Firebase services in your Android application you may have to add the google-services plugin to your build.gradle file:

dependencies {
    classpath 'com.google.gms:google-services:4.5.0'
    // ...
}

google-services প্লাগইনটির দুটি প্রধান কাজ রয়েছে:

  1. Process the google-services.json file and produce Android resources that can be used in your application's code. See Adding the JSON File more information.
  2. Add dependencies for basic libraries required for the services you have enabled. This step requires that you apply the Google Services Gradle plugin in your app/build.gradle file, like so:
    apply plugin: 'com.google.gms.google-services'

    You can see the result of this step by running ./gradlew :app:dependencies .

JSON ফাইল যোগ করা

The google-services.json file is generally placed in the app/ directory (at the root of the Android Studio app module). As of version 2.2.0 the plugin supports build type and product flavor specific JSON files. All of the following directory structures are valid:

// dogfood and release are build types.
app/
    google-services.json
    src/dogfood/google-services.json
    src/release/google-services.json
    ...

Note: Providing a google-services.json file in the release directory allows you to maintain a separate Firebase project for your production APKs.

যখন পণ্যের ফ্লেভারগুলো ব্যবহৃত হয়, তখন এই আরও জটিল ডিরেক্টরি কাঠামোগুলোও বৈধ থাকে।

// free and paid are product flavors.
app/
    google-services.json
    src/dogfood/paid/google-services.json
    src/release/free/google-services.json
    ...

Processing the JSON File

google-services.json ফাইলটির মৌলিক কাঠামোটি নিম্নরূপ:

{
  "project_info": {...},
  "client": [...],
}

project_info অবজেক্টটিতে আপনার প্রজেক্ট সম্পর্কিত সাধারণ তথ্য থাকে, অন্যদিকে client অ্যারের প্রতিটি সদস্যে সেইসব ক্লায়েন্টের (অ্যান্ড্রয়েড অ্যাপ) তথ্য থাকে, যেগুলো আপনি প্রজেক্টে যুক্ত করেছেন।

আপনার অ্যান্ড্রয়েড অ্যাপের জন্য JSON ফাইলটি প্রসেস করার সময়, প্লাগইনটি নিম্নলিখিত লজিকের উপর ভিত্তি করে শুধুমাত্র সেই client অবজেক্টটি ব্যবহার করে যা আপনার প্যাকেজ নামের (বর্তমান বিল্ড টাইপের জন্য) সাথে মেলে:

  • For each member of the client array:
    • client_info/android_client_info/package_name এর মান যাচাই করুন
    • যদি প্যাকেজের নামটি এই মানের সাথে মিলে যায়, তাহলে মেম্বার অবজেক্টটি ফেরত দিন।
  • যদি client কোনো সদস্য প্যাকেজ নামের সাথে মেলে না, তাহলে একটি ব্যতিক্রম প্রদর্শিত হয়।

এই ডকুমেন্টের বাকি অংশে, আমরা {YOUR_CLIENT} ব্যবহার করে উপরোক্ত পদ্ধতি দ্বারা নির্ধারিত client অ্যারের সদস্যকে বোঝাব।

JSON প্রক্রিয়াকরণের প্রধান ফলাফল হলো দুটি XML ফাইল তৈরি করা, যেগুলোকে আপনি আপনার জাভা কোডে অ্যান্ড্রয়েড রিসোর্স হিসেবে ব্যবহার করতে পারবেন। নিচে প্রতিটি ফাইলের একটি করে উদাহরণ দেওয়া হলো:

app/build/generated/res/google-services/{build_type}/values/values.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <! -- Present in all applications -->
    <string name="google_app_id" translatable="false">1:1035469437089:android:73a4fb8297b2cd4f</string>

    <! -- Present in applications with the appropriate services configured -->
    <string name="gcm_defaultSenderId" translatable="false">1035469437089</string>
    <string name="default_web_client_id" translatable="false">337894902146-e4uksm38sne0bqrj6uvkbo4oiu4hvigl.apps.googleusercontent.com</string>
    <string name="ga_trackingId" translatable="false">UA-65557217-3</string>
    <string name="firebase_database_url" translatable="false">https://example-url.firebaseio.com</string>
    <string name="google_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
    <string name="google_crash_reporting_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
    <string name="project_id" translatable="false">mydemoapp</string>

</resources>

app/build/generated/res/google-services/{flavor}/{build_type}/xml/global_tracker.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="ga_trackingId" translatable="false">UA-65557218-3</string>
</resources>

XML ফাইলগুলোর প্রতিটি ভ্যালু নিচের লোকেশনগুলোতে থাকা google-services.json ফাইলের মধ্যে পাওয়া যাবে। যদি আপনার অ্যান্ড্রয়েড প্রজেক্টের কোনো কনফিগারেশনের কারণে আপনি google-services প্লাগইনটি ব্যবহার করতে না পারেন, তাহলে আপনি এই ভ্যালুগুলো ব্যবহার করে নিরাপদে ম্যানুয়ালি XML ফাইলগুলো পুনরায় তৈরি করতে পারেন:

google_app_id :

{YOUR_CLIENT}/client_info/mobilesdk_app_id

gcm_defaultSenderId :

project_info/project_number

default_web_client_id :

{YOUR_CLIENT}/oauth_client/client_id (client_type == 3)

ga_trackingId :

{YOUR_CLIENT}/services/analytics-service/analytics_property/tracking_id

firebase_database_url :

project_info/firebase_url

google_api_key :

{YOUR_CLIENT}/api_key/current_key

google_crash_reporting_api_key :

{YOUR_CLIENT}/api_key/current_key

project_id :

project_info/project_id

Troubleshooting

বিল্ড করার সময় আমি নিম্নলিখিত ত্রুটি বার্তাটি পাই: "মডিউলের রুট ফোল্ডারে google-services.json ফাইলটি অনুপস্থিত। এটি ছাড়া গুগল সার্ভিসেস প্লাগইনটি কাজ করতে পারবে না"।

A: The Firebase console will help you download the google-services.json . In addition, the Quickstart guides for most APIs have instructions for generating this file. Once you have downloaded the google-services.json file, copy it into the app/ folder of your Android Studio project, or into the app/src/{build_type} folder if you are using multiple build types.

Q: I can't find the symbol "R.string.gcm_defaultSenderId", "R.xml.global_tracker", etc.

এ: নিশ্চিত করুন যে আপনার build.gradle ফাইলের প্যাকেজ নামটি google-services.json ফাইলটি তৈরি করার সময় আপনার দেওয়া প্যাকেজ নামের সাথে মেলে। আপনি যদি নিশ্চিত না হন, তবে আবার গেটিং স্টার্টেড ফ্লোটি অনুসরণ করুন এবং একটি নতুন json ফাইল তৈরি করুন।