Android Studio प्रोजेक्ट सेट अप करना

Places SDK for Android का इस्तेमाल करने के लिए, अपने ऐप्लिकेशन को कॉन्फ़िगर करने के लिए, यह तरीका अपनाएं. Places SDK for Android का इस्तेमाल करने वाले सभी ऐप्लिकेशन के लिए, ये ज़रूरी हैं.

पहला चरण: Android Studio सेट अप करना

इस दस्तावेज़ में, Android Studio Hedgehog और Android Gradle प्लगिन के वर्शन 8.2 का इस्तेमाल करके, डेवलपमेंट एनवायरमेंट के बारे में बताया गया है.

दूसरा चरण. एसडीके टूल सेट अप करना

Android के लिए Places SDK की लाइब्रेरी, Google की Maven रिपॉज़िटरी पर उपलब्ध है. अपने ऐप्लिकेशन में SDK टूल जोड़ने के लिए, यह तरीका अपनाएं:

  1. अपनी टॉप-लेवल settings.gradle.kts फ़ाइल में, pluginManagement ब्लॉक के तहत Gradle प्लग इन पोर्टल, Google Maven रिपॉज़िटरी, और Maven सेंट्रल रिपॉज़िटरी को शामिल करें. pluginManagement ब्लॉक, स्क्रिप्ट में मौजूद अन्य स्टेटमेंट से पहले दिखना चाहिए.
    pluginManagement {
        repositories {
            google()
            mavenCentral()
            gradlePluginPortal()
        }
    } 
  2. अपनी टॉप-लेवल settings.gradle.kts फ़ाइल में, dependencyResolutionManagement ब्लॉक के तहत Google की Maven रिपॉज़िटरी और Maven सेंट्रल रिपॉज़िटरी को शामिल करें:
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
        repositories {
            google()
            mavenCentral()
        }
    } 
  3. अपने मॉड्यूल-लेवल की build.gradle.kts फ़ाइल के dependencies सेक्शन में, Places SDK for Android के लिए डिपेंडेंसी जोड़ें:

    ग्रूवी

    dependencies {
        implementation(platform("org.jetbrains.kotlin:kotlin-bom:$kotlin_version"))
        implementation("com.google.android.libraries.places:places:3.5.0")
    }

    Kotlin

              dependencies {
    
        // Places and Maps SDKs
        implementation("com.google.android.libraries.places:places:6.0.2")
    }
  4. मॉड्यूल-लेवल की build.gradle.kts फ़ाइल में, compileSdk और minSdk को इन वैल्यू पर सेट करें:

    ग्रूवी

    android {
        compileSdk 34
    
        defaultConfig {
            minSdk 23
            // ...
        }
    }

    Kotlin

    android {
        compileSdk = 34
    
        defaultConfig {
            minSdk = 23
            // ...
        }
    }
  5. मॉड्यूल-लेवल की build.gradle फ़ाइल के buildFeatures सेक्शन में, BuildConfig क्लास जोड़ें. इसका इस्तेमाल, इस प्रोसेस में बाद में तय की गई मेटाडेटा वैल्यू को ऐक्सेस करने के लिए किया जाता है:

    ग्रूवी

    android {
      // ...
      buildFeatures {
        buildConfig true
        // ...
      }
    }

    Kotlin

    android {
      // ...
      buildFeatures {
        buildConfig = true
        // ...
      }
    }

तीसरा चरण: प्रोजेक्ट में अपना एपीआई पासकोड जोड़ना

इस सेक्शन में, एपीआई पासकोड को सेव करने का तरीका बताया गया है, ताकि आपका ऐप्लिकेशन उसे सुरक्षित तरीके से रेफ़र कर सके. आपको अपने वर्शन कंट्रोल सिस्टम में एपीआई पासकोड की जांच नहीं करनी चाहिए. इसलिए, हमारा सुझाव है कि आप इसे अपने प्रोजेक्ट की रूट डायरेक्ट्री में मौजूद secrets.properties फ़ाइल में सेव करें और उस फ़ाइल को वर्शन कंट्रोल से बाहर रखें. Gradle प्रॉपर्टी फ़ाइलों के बारे में ज़्यादा जानने के लिए, Gradle प्रॉपर्टी फ़ाइलें देखें.

इस टास्क को आसान बनाने के लिए, हमारा सुझाव है कि आप Android के लिए सीक्रेट ग्रेडल प्लग इन का इस्तेमाल करें.

Android के लिए Secrets Gradle Plugin इंस्टॉल करने और अपना एपीआई पासकोड सेव करने के लिए:

  1. Android Studio में, अपनी टॉप-लेवल build.gradle.kts या build.gradle फ़ाइल खोलें और plugins ब्लॉक में यह कोड जोड़ें.

    Kotlin

    plugins {
        // ...
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") version "2.0.1" apply false
    }

    ग्रूवी

    plugins {
        // ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false
    }
  2. मॉड्यूल-लेवल की build.gradle.kts या build.gradle फ़ाइल खोलें और plugins एलिमेंट में यह कोड जोड़ें.

    Kotlin

    plugins {
        id("com.android.application")
        // ...
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    }

    ग्रूवी

    plugins {
        id 'com.android.application'
        // ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    }
  3. अगर Android Studio का इस्तेमाल किया जा रहा है, तो अपने प्रोजेक्ट को Gradle के साथ सिंक करें.
  4. अपनी टॉप-लेवल डायरेक्ट्री में secrets.properties फ़ाइल खोलें. इसके बाद, यह कोड जोड़ें. YOUR_API_KEY को अपने एपीआई पासकोड से बदलें. secrets.properties को अपनी .gitignore फ़ाइल में जोड़ना न भूलें. इसके अलावा, इसे अपने वर्शन कंट्रोल सिस्टम से बाहर रखें, ताकि आप वर्शन कंट्रोल में अपना एपीआई पासकोड न डालें.
    PLACES_API_KEY=YOUR_API_KEY
  5. अपनी टॉप-लेवल डायरेक्ट्री में local.defaults.properties फ़ाइल बनाएं. यह वही फ़ोल्डर होना चाहिए जिसमें secrets.properties फ़ाइल है. इसके बाद, यह कोड जोड़ें.

    PLACES_API_KEY=DEFAULT_API_KEY

    इस फ़ाइल का मकसद, एपीआई पासकोड के लिए बैकअप लोकेशन उपलब्ध कराना है. ऐसा तब किया जाता है, जब secrets.properties फ़ाइल न मिले, ताकि बिल्ड फ़ेल न हों. ऐसा तब हो सकता है, जब आपने ऐप्लिकेशन को किसी ऐसे वर्शन कंट्रोल सिस्टम से क्लोन किया हो जिसमें secrets.properties शामिल न हो और आपने अब तक अपने एपीआई पासकोड को उपलब्ध कराने के लिए, डिवाइस पर secrets.properties फ़ाइल न बनाई हो.

  6. Android Studio में, मॉड्यूल-लेवल की build.gradle.kts या build.gradle फ़ाइल खोलें और secrets प्रॉपर्टी में बदलाव करें. अगर secrets प्रॉपर्टी मौजूद नहीं है, तो उसे जोड़ें.

    प्लग इन की प्रॉपर्टी में बदलाव करके, propertiesFileName को "secrets.properties" पर सेट करें. साथ ही, defaultPropertiesFileName को "local.defaults.properties" पर सेट करें और अन्य प्रॉपर्टी सेट करें.

    Kotlin

    secrets {
        // To add your Maps API key to this project:
        // 1. If the secrets.properties file does not exist, create it in the root directory (the same folder as the root local.properties file).
        // 2. Add this line, where YOUR_API_KEY is your API key:
        //        MAPS_API_KEY=YOUR_API_KEY
        propertiesFileName = "secrets.properties"
    
        // A properties file containing default secret values. This file can be
        // checked in version control.
        defaultPropertiesFileName = "local.defaults.properties"
    }

    ग्रूवी

    secrets {
        // To add your Maps API key to this project:
        // 1. If the secrets.properties file does not exist, create it in the root directory (the same folder as the root local.properties file).
        // 2. Add this line, where YOUR_API_KEY is your API key:
        //        MAPS_API_KEY=YOUR_API_KEY
        propertiesFileName = "secrets.properties"
    
        // A properties file containing default secret values. This file can be
        // checked in version control.
        defaultPropertiesFileName = "local.defaults.properties"
    }

चौथा चरण. Places API क्लाइंट की प्रोसेस शुरू करना

किसी गतिविधि या फ़्रैगमेंट में Places SDK for Android को शुरू करने के लिए, Places.initializeWithNewPlacesApiEnabled() को कॉल करते समय एपीआई पासकोड पास करें:

Kotlin

    // Define a variable to hold the Places API key.
    val apiKey = BuildConfig.PLACES_API_KEY

    // Log an error if apiKey is not set.
    if (apiKey.isEmpty() || apiKey == "DEFAULT_API_KEY") {
        Log.e("Places test", "No api key")
        finish()
        return
    }

    // Initialize the SDK
    Places.initializeWithNewPlacesApiEnabled(applicationContext, apiKey)

    // Create a new PlacesClient instance
    val placesClient = Places.createClient(this)
    

Java

    // Define a variable to hold the Places API key.
    String apiKey = BuildConfig.PLACES_API_KEY;

    // Log an error if apiKey is not set.
    if (TextUtils.isEmpty(apiKey) || apiKey.equals("DEFAULT_API_KEY")) {
      Log.e("Places test", "No api key");
      finish();
      return;
    }

    // Initialize the SDK
    Places.initializeWithNewPlacesApiEnabled(getApplicationContext(), apiKey);

    // Create a new PlacesClient instance
    PlacesClient placesClient = Places.createClient(this);
    

अब Android के लिए Places SDK का इस्तेमाल किया जा सकता है!

पांचवां चरण: Android डिवाइस सेट अप करना

Places SDK for Android का इस्तेमाल करने वाले ऐप्लिकेशन को चलाने के लिए, आपको उसे Android 6.0 या इसके बाद के वर्शन पर आधारित Android डिवाइस या Android Emulator पर डिप्लॉय करना होगा. साथ ही, उसमें Google API शामिल होने चाहिए.

  • Android डिवाइस का इस्तेमाल करने के लिए, हार्डवेयर डिवाइस पर ऐप्लिकेशन चलाएं में दिए गए निर्देशों का पालन करें.
  • Android Emulator का इस्तेमाल करने के लिए, वर्चुअल डिवाइस बनाया जा सकता है. इसके बाद, Android Studio के साथ मिलने वाले डिवाइस मैनेजर का इस्तेमाल करके, एम्युलेटर को इंस्टॉल किया जा सकता है.

अगले चरण

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