अपने ऐप्लिकेशन को Places SDK for Android का इस्तेमाल करने के लिए कॉन्फ़िगर करने के लिए, यह तरीका अपनाएं. Places SDK for Android का इस्तेमाल करने वाले सभी ऐप्लिकेशन के लिए, यह तरीका अपनाना ज़रूरी है.
पहला चरण: Android Studio सेट अप करना
इस दस्तावेज़ में, Android Studio Hedgehog और Android Gradle प्लगिन के 8.2 वर्शन का इस्तेमाल करके, डेवलपमेंट एनवायरमेंट सेट अप करने का तरीका बताया गया है.
.दूसरा चरण. एसडीके सेट अप करना
Places SDK for Android की लाइब्रेरी, Google की मेवन रिपॉज़िटरी पर उपलब्ध है. अपने ऐप्लिकेशन में एसडीके जोड़ने के लिए, यह तरीका अपनाएं:
- अपनी टॉप-लेवल
settings.gradle.ktsफ़ाइल में, Gradle प्लगिन पोर्टल, Google Maven रिपॉज़िटरी, और Maven सेंट्रल रिपॉज़िटरी कोpluginManagementब्लॉक में शामिल करें. ThepluginManagementब्लॉक स्क्रिप्ट में किसी भी अन्य स्टेटमेंट से पहले दिखना चाहिए.pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } }
- अपनी टॉप-लेवल
settings.gradle.ktsफ़ाइल में, Google की Maven रिपॉज़िटरी और Maven सेंट्रल रिपॉज़िटरी कोdependencyResolutionManagementब्लॉक में शामिल करें:dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS) repositories { google() mavenCentral() } }
-
अपने मॉड्यूल-लेवल की
build.gradle.ktsफ़ाइल केdependenciesसेक्शन में, Places SDK for Android की डिपेंडेंसी जोड़ें:Groovy
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") }
- अपने मॉड्यूल-लेवल की
build.gradle.ktsफ़ाइल में,compileSdkऔरminSdkको ये वैल्यू सेट करें:Groovy
android { compileSdk 34 defaultConfig { minSdk 23 // ... } }
Kotlin
android { compileSdk = 34 defaultConfig { minSdk = 23 // ... } }
- अपने मॉड्यूल-लेवल की
build.gradleफ़ाइल केbuildFeaturesसेक्शन में,BuildConfigक्लास जोड़ें. इसका इस्तेमाल, इस तरीके में बाद में तय की गई मेटाडेटा वैल्यू को ऐक्सेस करने के लिए किया जाता है:Groovy
android { // ... buildFeatures { buildConfig true // ... } }
Kotlin
android { // ... buildFeatures { buildConfig = true // ... } }
तीसरा चरण: प्रोजेक्ट में अपना एपीआई पासकोड जोड़ना
इस सेक्शन में, एपीआई पासकोड को सेव करने का तरीका बताया गया है, ताकि आपका ऐप्लिकेशन इसे सुरक्षित तरीके से रेफ़र कर सके. आपको अपने वर्शन कंट्रोल सिस्टम में एपीआई पासकोड की जांच नहीं करनी चाहिए. इसलिए, हमारा सुझाव है कि इसे अपने प्रोजेक्ट की रूट डायरेक्ट्री में मौजूद secrets.properties फ़ाइल में सेव करें और उस फ़ाइल को वर्शन कंट्रोल से बाहर रखें. Gradle
की प्रॉपर्टी फ़ाइलों के बारे में ज़्यादा जानने के लिए, Gradle की प्रॉपर्टी फ़ाइलें देखें.
इस काम को आसान बनाने के लिए, हमारा सुझाव है कि आप Android के लिए सीक्रेट ग्रेडल प्लगिनका इस्तेमाल करें.
Android के लिए सीक्रेट ग्रेडल प्लगिन इंस्टॉल करने और अपना एपीआई पासकोड सेव करने के लिए:
-
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 }
Groovy
plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false }
-
अपने मॉड्यूल-लेवल की
build.gradle.ktsयाbuild.gradleफ़ाइल खोलें औरpluginsएलिमेंट में यह कोड जोड़ें.Kotlin
plugins { id("com.android.application") // ... id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
Groovy
plugins { id 'com.android.application' // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
- अगर Android Studio का इस्तेमाल किया जा रहा है, तो अपने प्रोजेक्ट को Gradle के साथ सिंक करें.
-
अपनी टॉप-लेवल डायरेक्ट्री में मौजूद
secrets.propertiesफ़ाइल खोलें. इसके बाद, यह कोड जोड़ें.YOUR_API_KEYको अपने एपीआई पासकोड से बदलें. पक्का करें कि आपनेsecrets.propertiesको अपनी.gitignoreफ़ाइल में जोड़ा हो. इसके अलावा, इसे अपने वर्शन कंट्रोल सिस्टम से बाहर रखें, ताकि वर्शन कंट्रोल में आपका एपीआई पासकोड शामिल न हो.PLACES_API_KEY=YOUR_API_KEY
-
अपनी टॉप-लेवल डायरेक्ट्री में
local.defaults.propertiesफ़ाइल बनाएं. यह फ़ाइल,secrets.propertiesफ़ाइल वाले फ़ोल्डर में होनी चाहिए. इसके बाद, यह कोड जोड़ें.PLACES_API_KEY=DEFAULT_API_KEY
इस फ़ाइल का मकसद, एपीआई पासकोड के लिए बैकअप की जगह उपलब्ध कराना है. ऐसा इसलिए, ताकि अगर
secrets.propertiesफ़ाइल न मिले, तो भी बिल्ड न रुकें. ऐसा तब हो सकता है, जब आपने किसी वर्शन कंट्रोल सिस्टम से ऐप्लिकेशन को क्लोन किया हो. इस सिस्टम मेंsecrets.propertiesफ़ाइल शामिल नहीं होती. साथ ही, आपने एपीआई पासकोड देने के लिए, स्थानीय तौर परsecrets.propertiesफ़ाइल नहीं बनाई है. -
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" }
Groovy
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);
अब Places SDK for Android का इस्तेमाल किया जा सकता है!
पांचवा चरण: Android डिवाइस सेट अप करना
Places SDK for Android का इस्तेमाल करने वाले किसी ऐप्लिकेशन को चलाने के लिए, आपको उसे Android डिवाइस या Android एम्युलेटर पर डिप्लॉय करना होगा. यह डिवाइस या एम्युलेटर, Android 6.0 या इसके बाद के वर्शन पर आधारित होना चाहिए. साथ ही, इसमें Google के एपीआई शामिल होने चाहिए.
- Android डिवाइस का इस्तेमाल करने के लिए, हार्डवेयर डिवाइस पर ऐप्लिकेशन चलाना लेख में दिए गए निर्देशों का पालन करें.
- Android एम्युलेटर का इस्तेमाल करने के लिए, वर्चुअल डिवाइस बनाया जा सकता है. साथ ही, Android Studio के साथ आने वाले डिवाइस मैनेजर का इस्तेमाल करके, एम्युलेटर इंस्टॉल किया जा सकता है.