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

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

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

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

दूसरा चरण. SDK टूल सेट अप करना

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

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

    ग्रूवी

    dependencies {
        // If updating kotlin-bom version number above, also edit project-level build.gradle definition of $kotlin_version variable
        implementation(platform("org.jetbrains.kotlin:kotlin-bom:$kotlin_version"))
        implementation 'com.google.android.libraries.places:places:3.3.0'
    }

    Kotlin

    dependencies {
        implementation(platform("org.jetbrains.kotlin:kotlin-bom:$kotlin_version"))
        implementation("com.google.android.libraries.places:places:3.3.0")
    }
  4. अपनी मॉड्यूल-लेवल build.gradle फ़ाइल में, compileSdk और minSdk को इन वैल्यू पर सेट करें:

    ग्रूवी

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

    Kotlin

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

    ग्रूवी

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

    Kotlin

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

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

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

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

अपने Google Maps प्रोजेक्ट में Android के लिए Secrets Gradle प्लग इन इंस्टॉल करने के लिए:

  1. Android Studio में, अपनी टॉप लेवल build.gradle या build.gradle.kts फ़ाइल खोलें और buildscript में जाकर, dependencies एलिमेंट में इस कोड को जोड़ें.

    ग्रूवी

    buildscript {
        dependencies {
            classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1"
        }
    }

    Kotlin

    buildscript {
        dependencies {
            classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1")
        }
    }
    
  2. अपने मॉड्यूल-लेवल की build.gradle फ़ाइल खोलें और plugins एलिमेंट में यह कोड जोड़ें.

    ग्रूवी

    plugins {
        // ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    }

    Kotlin

    plugins {
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    }
  3. अपनी मॉड्यूल-लेवल की build.gradle फ़ाइल में, पक्का करें कि targetSdk और compileSdk को 34 पर सेट किया गया हो.
  4. फ़ाइल सेव करें और Gredle के साथ अपना प्रोजेक्ट सिंक करें.
  5. अपनी टॉप-लेवल डायरेक्ट्री में secrets.properties फ़ाइल खोलें और फिर यह कोड जोड़ें. YOUR_API_KEY को अपनी एपीआई पासकोड से बदलें. अपनी कुंजी को इस फ़ाइल में सेव करें, क्योंकि secrets.properties को वर्शन कंट्रोल सिस्टम में चेक नहीं किया जाता है.
    PLACES_API_KEY=YOUR_API_KEY
  6. फ़ाइल सेव करें.
  7. अपनी टॉप-लेवल डायरेक्ट्री में local.defaults.properties फ़ाइल बनाएं. यह वही फ़ोल्डर है जिसमें secrets.properties फ़ाइल है. इसके बाद, यह कोड जोड़ें.

    PLACES_API_KEY=DEFAULT_API_KEY

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

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

    प्लगिन की प्रॉपर्टी में बदलाव करके, propertiesFileName को secrets.properties पर सेट करें, defaultPropertiesFileName को local.defaults.properties पर सेट करें, और कोई दूसरी प्रॉपर्टी सेट करें.

    ग्रूवी

    secrets {
        // Optionally specify a different file name containing your secrets.
        // The plugin defaults to "local.properties"
        propertiesFileName = "secrets.properties"
    
        // A properties file containing default secret values. This file can be
        // checked in version control.
        defaultPropertiesFileName = "local.defaults.properties"
    
        // Configure which keys should be ignored by the plugin by providing regular expressions.
        // "sdk.dir" is ignored by default.
        ignoreList.add("keyToIgnore") // Ignore the key "keyToIgnore"
        ignoreList.add("sdk.*")       // Ignore all keys matching the regexp "sdk.*"
    }
            

    Kotlin

    secrets {
        // Optionally specify a different file name containing your secrets.
        // The plugin defaults to "local.properties"
        propertiesFileName = "secrets.properties"
    
        // A properties file containing default secret values. This file can be
        // checked in version control.
        defaultPropertiesFileName = "local.defaults.properties"
    
        // Configure which keys should be ignored by the plugin by providing regular expressions.
        // "sdk.dir" is ignored by default.
        ignoreList.add("keyToIgnore") // Ignore the key "keyToIgnore"
        ignoreList.add("sdk.*")       // Ignore all keys matching the regexp "sdk.*"
    }
            

चरण 4. Places API क्लाइंट को शुरू करें

किसी गतिविधि या फ़्रैगमेंट में Android के लिए Places SDK टूल शुरू करें. पहले आपको यह तय करना होगा कि SDK के किस वर्शन का इस्तेमाल किया जाए: Android के लिए Places SDK टूल या Android के लिए Places SDK टूल (नया). प्रॉडक्ट के वर्शन के बारे में ज़्यादा जानकारी पाने के लिए, अपने SDK टूल का वर्शन चुनना लेख पढ़ें.

नीचे दिए गए उदाहरण में, दोनों वर्शन के लिए SDK टूल शुरू करने का तरीका दिखाया गया है.

Android के लिए Places SDK टूल (नया)

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 टूल

Places.initialize() को कॉल करते समय, एपीआई पासकोड पास करें:

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.initialize(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.initialize(getApplicationContext(), apiKey);

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

अब आप Android के लिए स्थल SDK का उपयोग शुरू करने के लिए तैयार हैं!

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

Android के लिए Places SDK टूल का इस्तेमाल करने वाले ऐप्लिकेशन को चलाने के लिए, आपको इसे किसी ऐसे Android डिवाइस या Android एम्युलेटर पर डिप्लॉय करना होगा जो Android 4.0 या इसके बाद वाले वर्शन पर आधारित हो और जिनमें Google API शामिल हों.

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

अगले चरण

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