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 फ़ाइल में, Google की Maven रिपॉज़िटरी और Maven सेंट्रल रिपॉज़िटरी को dependencyResolutionManagement ब्लॉक में शामिल करें:
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
        }
    } 
  3. अपने मॉड्यूल-लेवल की build.gradle फ़ाइल के dependencies सेक्शन में, Android के लिए Places 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.5.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 प्रॉपर्टी फ़ाइलें देखें.

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

अपने 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. फ़ाइल सेव करें और अपने प्रोजेक्ट को Gradle के साथ सिंक करें.
  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 के लिए Places SDK का इस्तेमाल शुरू करने के लिए तैयार हैं!

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

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

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

अगले चरण

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