अपना प्रोजेक्ट सेट करें -- 4.99 और इससे पहले के वर्शन

इस गाइड में Android के लिए नेविगेशन SDK टूल का इस्तेमाल करने के लिए, बिल्ड कॉन्फ़िगरेशन से जुड़ी ज़रूरी शर्तों की जानकारी दी गई है. निर्देशों में यह माना जाता है कि आपके डिवाइस पर Android IDE इंस्टॉल है और आपको Android डेवलपमेंट के बारे में जानकारी है.

नेविगेशन SDK टूल इस्तेमाल करने के लिए ज़रूरी शर्तें

ये ज़रूरी शर्तें, Android के 4.99 और उससे पहले के वर्शन के लिए बने नेविगेशन SDK टूल पर लागू होती हैं.

  • Google Cloud Console प्रोजेक्ट, जिसमें नेविगेशन SDK टूल चालू हो. प्रावधान करने के लिए, अपने Google Maps Platform प्रतिनिधि से संपर्क करें.

  • आपके ऐप्लिकेशन को, एपीआई लेवल 30 या उसके बाद के लेवल को टारगेट करना चाहिए.

  • नेविगेशन SDK टूल की मदद से बना ऐप्लिकेशन चलाने के लिए, Android डिवाइस पर Google Play services इंस्टॉल और चालू होनी चाहिए.

  • एट्रिब्यूशन और लाइसेंस देने का टेक्स्ट ऐप्लिकेशन में जोड़ना ज़रूरी है.

अपने प्रोजेक्ट सेट अप करें: Cloud Console प्रोजेक्ट और Android प्रोजेक्ट

ऐप्लिकेशन बनाने या उसकी जांच करने से पहले, आपको Cloud Console प्रोजेक्ट बनाना होगा और एपीआई कुंजी के क्रेडेंशियल जोड़ने होंगे. नेविगेशन SDK टूल को ऐक्सेस करने के लिए, प्रोजेक्ट में प्रावधान होना चाहिए. Cloud Console प्रोजेक्ट में मौजूद सभी कुंजियों को नेविगेशन SDK टूल का एक जैसा ऐक्सेस दिया जाता है. किसी कुंजी के साथ एक से ज़्यादा डेवलपमेंट प्रोजेक्ट जुड़े हो सकते हैं. अगर आपके पास पहले से कोई कंसोल प्रोजेक्ट है, तो अपने मौजूदा प्रोजेक्ट में कुंजी जोड़ी जा सकती है.

सेट अप करने के लिए

  1. अपने पसंदीदा वेब ब्राउज़र में, Cloud Console में साइन इन करें और अपना Cloud Console प्रोजेक्ट बनाएं.
  2. Android Studio जैसे अपने IDE में, Android ऐप्लिकेशन डेवलपमेंट प्रोजेक्ट बनाएं और पैकेज का नाम नोट करें.
  3. अपने Cloud Console प्रोजेक्ट के लिए, नेविगेशन SDK टूल का ऐक्सेस देने के लिए, अपने Google Maps Platform के प्रतिनिधि से संपर्क करें.
  4. अपने वेब ब्राउज़र में Cloud Console के डैशबोर्ड पर, एपीआई पासकोड जनरेट करने के लिए क्रेडेंशियल बनाएं. हालांकि, यह ज़रूरी है कि आपके पास पाबंदियों वाली कुंजी जनरेट हो.
  5. एपीआई मुख्य पेज पर, ऐप्लिकेशन पाबंदियां सेक्शन में Android ऐप्लिकेशन पर क्लिक करें.
  6. पैकेज का नाम और फ़िंगरप्रिंट जोड़ें पर क्लिक करें. इसके बाद, अपने डेवलपमेंट प्रोजेक्ट का पैकेज नाम और उस कुंजी के लिए SHA-1 फ़िंगरप्रिंट डालें.
  7. सेव करें पर क्लिक करें.

अपने प्रोजेक्ट में नेविगेशन SDK टूल जोड़ें

नेविगेशन SDK टूल, Maven के ज़रिए या AAR बंडल के तौर पर उपलब्ध है. अपना डेवलपमेंट प्रोजेक्ट बनाने के बाद, SDK टूल को उसमें इंटिग्रेट करने के लिए, इनमें से किसी एक तरीके का इस्तेमाल किया जा सकता है.

नीचे, google() मेवन रिपॉज़िटरी का इस्तेमाल किया गया है. यह आपके प्रोजेक्ट में नेविगेशन SDK टूल जोड़ने का सबसे आसान और सुझाया गया तरीका है.

  1. अपने Gradle या Maven कॉन्फ़िगरेशन में नीचे दी गई डिपेंडेंसी जोड़ें. साथ ही, Android के लिए नेविगेशन SDK के मनमुताबिक वर्शन के लिए, VERSION_NUMBER प्लेसहोल्डर को बदलें.

    ग्रेडल

    अपने मॉड्यूल-लेवल build.gradle में यह जोड़ें:

    dependencies {
      ...
      implementation 'com.google.android.libraries.navigation:navigation:VERSION_NUMBER'
    }
    

    अगर मूल Maven का डेटा स्टोर करने की जगह से अपग्रेड किया जा रहा है, तो ध्यान रखें कि ग्रुप और आर्टफ़ैक्ट के नाम बदल गए हैं. साथ ही, com.google.cloud.artifactregistry.gradle-plugin प्लगिन की अब ज़रूरत नहीं है.

    साथ ही, इन चीज़ों को अपने टॉप-लेवल build.gradle में जोड़ें:

    allprojects {
       ...
       // Required: you must exclude the Google Play service Maps SDK from
       // your transitive dependencies. This is to ensure there won't be
       // multiple copies of Google Maps SDK in your binary, as the Navigation
       // SDK already bundles the Google Maps SDK.
       configurations {
           implementation {
               exclude group: 'com.google.android.gms', module: 'play-services-maps'
           }
       }
    }
    

    Maven

    अपने pom.xml में यह जोड़ें:

    <dependencies>
      ...
      <dependency>
        <groupId>com.google.android.libraries.navigation</groupId>
        <artifactId>navigation</artifactId>
        <version>VERSION_NUMBER</version>
      </dependency>
    </dependencies>
    

    अगर आपके पास Maps SDK टूल का इस्तेमाल करने वाली कोई डिपेंडेंसी है, तो आपको Maps के SDK टूल पर निर्भर हर डिपेंडेंसी को बाहर रखना होगा.

    <dependencies>
      <dependency>
      <groupId>project.that.brings.in.maps</groupId>
      <artifactId>MapsConsumer</artifactId>
      <version>1.0</version>
        <exclusions>
          <!-- Navigation SDK already bundles Maps SDK. You must exclude it to prevent duplication-->
          <exclusion>  <!-- declare the exclusion here -->
            <groupId>com.google.android.gms</groupId>
            <artifactId>play-services-maps</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
    </dependencies>
    

v4.5 से पहले के वर्शन या ड्राइवर SDK टूल के साथ, नेविगेशन SDK के लिए Maven का इस्तेमाल करना

नेविगेशन SDK टूल, ओरिजनल Maven रिपॉज़िटरी के ज़रिए, बाकी v4 वर्शन के ज़रिए उपलब्ध रहेगा. यह वही लाइब्रेरी है जिसमें ऊपर दिए गए वर्शन की तरह सभी अपडेट किए गए हैं. साथ ही, ट्रांज़िशन के दौरान यह ड्राइवर SDK और अन्य लाइब्रेरी के साथ काम करती है. इस डिपेंडेंसी का इस्तेमाल करने के लिए, कंपाइल करते समय gcloud से अपने क्लाउड प्रोजेक्ट में लॉग इन करना होगा.

  1. Google के Maven का डेटा स्टोर करने की जगह को ऐक्सेस करने के लिए, अपना एनवायरमेंट सेट अप करें. इसकी जानकारी, Consumer SDK दस्तावेज़ के ज़रूरी शर्तें सेक्शन में दी गई है. नेविगेशन SDK टूल के ऐक्सेस को वर्कस्पेस ग्रुप से कंट्रोल किया जाता है.
  2. अपने Gradle या Maven कॉन्फ़िगरेशन में यह डिपेंडेंसी जोड़ें. इससे नेविगेशन SDK टूल के मनचाहे वर्शन की जगह VERSION_NUMBER प्लेसहोल्डर दिखेगा.

    ग्रेडल

    अपने मॉड्यूल-लेवल build.gradle में यह जोड़ें:

    dependencies {
      ...
      implementation 'com.google.android.maps:navsdk:VERSION_NUMBER'
    }
    

    साथ ही, इन चीज़ों को अपने टॉप-लेवल build.gradle में जोड़ें:

    allprojects {
       ...
       // Required: you must exclude the Google Play service Maps SDK from
       // your transitive dependencies. This is to ensure there won't be
       // multiple copies of Google Maps SDK in your binary, as the Navigation
       // SDK already bundles the Google Maps SDK.
       configurations {
           implementation {
               exclude group: 'com.google.android.gms', module: 'play-services-maps'
           }
       }
    }
    

    Maven

    अपने pom.xml में यह जोड़ें:

    <dependencies>
      ...
      <dependency>
        <groupId>com.google.android.maps</groupId>
        <artifactId>navsdk</artifactId>
        <version>VERSION_NUMBER</version>
      </dependency>
    </dependencies>
    

    अगर आपके पास Maps SDK टूल का इस्तेमाल करने वाली कोई डिपेंडेंसी है, तो आपको Maps के SDK टूल पर निर्भर हर डिपेंडेंसी को बाहर रखना होगा.

    <dependencies>
      <dependency>
      <groupId>project.that.brings.in.maps</groupId>
      <artifactId>MapsConsumer</artifactId>
      <version>1.0</version>
        <exclusions>
          <!-- Navigation SDK already bundles Maps SDK. You must exclude it to prevent duplication-->
          <exclusion>  <!-- declare the exclusion here -->
            <groupId>com.google.android.gms</groupId>
            <artifactId>play-services-maps</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
    </dependencies>
    

नेविगेशन SDK टूल, AAR बंडल के तौर पर भी उपलब्ध है. डेवलपमेंट प्रोजेक्ट बनाने के बाद, SDK टूल को इंटिग्रेट किया जा सकता है. ये निर्देश आपके IDE के लिए Android Studio का इस्तेमाल करते हैं.

  1. शेयर की गई Google Drive से नेविगेशन SDK का सबसे नया वर्शन डाउनलोड करें और उसे अनज़िप करें. अगर आपके पास ऐक्सेस नहीं है, तो अपने प्रतिनिधि से संपर्क करें.

  2. Android Studio में, कोई प्रोजेक्ट खोलें और SDK Manager का इस्तेमाल करके, Google Play services का पैकेज जोड़ें.

  3. ZIP फ़ाइल डायरेक्ट्री से, libs/google_navigation_navmap.aar को अपने प्रोजेक्ट की app/libs डायरेक्ट्री में कॉपी करें.

  4. अपने मॉड्यूल-लेवल build.gradle में यह जोड़ें:

    implementation(name: 'google_navigation_navmap', ext: 'aar')
    

    साथ ही, इन चीज़ों को अपने टॉप-लेवल build.gradle में जोड़ें:

    allprojects {
        ...
        // Required: you must exclude the Google Play service Maps SDK from
        // your transitive dependencies. This is to ensure there won't be
        // multiple copies of Google Maps SDK in your binary, as the Navigation
        // SDK already bundles the Google Maps SDK.
        configurations {
            implementation {
                exclude group: 'com.google.android.gms', module: 'play-services-maps'
            }
        }
    }
    

बिल्ड को कॉन्फ़िगर करें

प्रोजेक्ट बनाने के बाद, नेविगेशन SDK टूल के सफल बिल्ड और इस्तेमाल के लिए सेटिंग कॉन्फ़िगर की जा सकती हैं.

स्थानीय प्रॉपर्टी अपडेट करें

  • gradle Scripts फ़ोल्डर में, local.properties फ़ाइल खोलें और android.useDeprecatedNdk=true जोड़ें.

Gradle बिल्ड स्क्रिप्ट अपडेट करना

  • build.gradle (Module:app) फ़ाइल खोलें और नेविगेशन SDK की ज़रूरतों को पूरा करने के लिए सेटिंग अपडेट करने के लिए, नीचे दिए गए दिशा-निर्देशों का पालन करें. साथ ही, ऑप्टिमाइज़ेशन के विकल्प सेट करें.

    नेविगेशन SDK टूल के लिए ज़रूरी सेटिंग

    1. minSdkVersion को 23 या उससे ज़्यादा पर सेट करें.
    2. targetSdkVersion को 30 या उससे ज़्यादा पर सेट करें.
    3. dexOptions की ऐसी सेटिंग जोड़ें जो javaMaxHeapSize को बढ़ाती है.
    4. अतिरिक्त लाइब्रेरी के लिए लोकेशन सेट करें.
    5. नेविगेशन SDK टूल के लिए, repositories और dependencies जोड़ें.
    6. डिपेंडेंसी में वर्शन नंबर को सबसे नए उपलब्ध वर्शन से बदलें.

    बिल्ड का समय कम करने के लिए वैकल्पिक सेटिंग

    • इस्तेमाल न होने वाले कोड और संसाधनों को डिपेंडेंसी से हटाने के लिए, R8/ProGuard पर कोड का साइज़ कम करने और रिसॉर्स का कम इस्तेमाल करने की सुविधा चालू करें. अगर R8/ProGuard को चलने में बहुत ज़्यादा समय लगता है, तो डेवलपमेंट से जुड़े काम के लिए multidex को चालू करें.
    • बिल्ड में शामिल भाषा के अनुवाद की संख्या कम करें: डेवलपमेंट के दौरान एक भाषा के लिए resConfigs सेट करें. फ़ाइनल बिल्ड के लिए, उन भाषाओं के लिए resConfigs सेट करें जिनका इस्तेमाल असल में किया जाता है. डिफ़ॉल्ट रूप से, Gradle में उन सभी भाषाओं के लिए संसाधन स्ट्रिंग शामिल होती हैं जो नेविगेशन SDK टूल पर काम करती हैं.

ऐप्लिकेशन के लिए, Gradle बिल्ड स्क्रिप्ट का उदाहरण नीचे दिया गया है. डिपेंडेंसी के अपडेट किए गए सेट के लिए, ऐप्लिकेशन के नमूने देखें, क्योंकि हो सकता है कि इस्तेमाल किए जा रहे नेविगेशन SDK टूल का वर्शन, इस दस्तावेज़ से थोड़ा आगे या पीछे हो.

apply plugin: 'com.android.application'
apply plugin: 'com.google.cloud.artifactregistry.gradle-plugin'

ext {
    androidxVersion = "1.0.0"
    lifecycle_version = "1.1.1"
}

android {
    compileSdkVersion 30
    buildToolsVersion '28.0.3'

    defaultConfig {
        applicationId "<your id>"
        // Navigation SDK supports SDK 23 and later.
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        // Set this to the languages you actually use, otherwise you'll include resource strings
        // for all languages supported by the Navigation SDK.
        resConfigs "en"
        multiDexEnabled true
    }

    dexOptions {
        // This increases the amount of memory available to the dexer. This is required to build
        // apps using the Navigation SDK.
        javaMaxHeapSize "4g"
    }
    buildTypes {
        // Run ProGuard. Note that the Navigation SDK includes its own ProGuard configuration.
        // The configuration is included transitively by depending on the Navigation SDK.
        // If the ProGuard step takes too long, consider enabling multidex for development work
        // instead.
        all {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

// This tells Gradle where to look to find additional libraries - in this case, the
// google_navigation_navmap.aar file.
repositories {
    flatDir {
        dirs 'libs'
    }
    google()

    // Required for accessing the Navigation SDK on Google's Maven repository.
    maven {
        url "artifactregistry://us-west2-maven.pkg.dev/gmp-artifacts/transportation"
    }
}

dependencies {
    // Include the Google Navigation SDK
    implementation 'com.google.android.maps:navsdk:4.4.0'

    // The included AAR file under libs can be used instead of the Maven repository.
    // Uncomment the line below and comment out the previous dependency to use
    // the AAR file instead. Ensure that you add the AAR file to the libs directory.
    // implementation(name: 'google_navigation_navmap', ext: 'aar')

    // These dependencies are required for the Navigation SDK to function
    // properly at runtime.
    implementation 'org.chromium.net:cronet-fallback:69.3497.100'
    // Optional for Cronet users:
    // implementation 'org.chromium.net:cronet-api:69.3497.100'
    implementation 'androidx.appcompat:appcompat:${androidxVersion}'
    implementation 'androidx.cardview:cardview:${androidxVersion}'
    implementation 'com.google.android.material:material:${androidxVersion}'
    implementation 'androidx.mediarouter:mediarouter:${androidxVersion}'
    implementation 'androidx.preference:preference:${androidxVersion}'
    implementation 'androidx.recyclerview:recyclerview:${androidxVersion}'
    implementation 'androidx.legacy:legacy-support-v4:${androidxVersion}'
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'com.github.bumptech.glide:okhttp-integration:4.9.0'
    implementation 'android.arch.lifecycle:common-java8:$lifecycle_version'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.google.android.datatransport:transport-api:2.2.0'
    implementation 'com.google.android.datatransport:transport-backend-cct:2.2.0'
    implementation 'com.google.android.datatransport:transport-runtime:2.2.0'
    implementation 'joda-time:joda-time:2.9.9'
    annotationProcessor 'androidx.annotation:annotation:1.1.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}

अपने ऐप्लिकेशन में एपीआई पासकोड जोड़ना

इस सेक्शन में बताया गया है कि अपनी एपीआई कुंजी को कैसे सेव करें, ताकि आपका ऐप्लिकेशन उसे सुरक्षित तरीके से इस्तेमाल कर सके. आपको अपने वर्शन कंट्रोल सिस्टम में एपीआई पासकोड की जांच नहीं करनी चाहिए. इसलिए, हमारा सुझाव है कि इसे 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 को वर्शन कंट्रोल सिस्टम में चेक नहीं किया जाता है.
    MAPS_API_KEY=YOUR_API_KEY
  6. फ़ाइल सेव करें.
  7. अपनी टॉप-लेवल डायरेक्ट्री में local.defaults.properties फ़ाइल बनाएं. यह वही फ़ोल्डर है जिसमें secrets.properties फ़ाइल है. इसके बाद, यह कोड जोड़ें.

    MAPS_API_KEY=DEFAULT_API_KEY

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

  8. फ़ाइल सेव करें.
  9. अपनी AndroidManifest.xml फ़ाइल में, com.google.android.geo.API_KEY पर जाएं और android:value attribute को अपडेट करें. अगर <meta-data> टैग मौजूद नहीं है, तो इसे <application> टैग के चाइल्ड के तौर पर बनाएं.
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="${MAPS_API_KEY}" />

    Note: com.google.android.geo.API_KEY is the recommended metadata name for the API key. A key with this name can be used to authenticate to multiple Google Maps-based APIs on the Android platform, including the Navigation SDK for Android. For backwards compatibility, the API also supports the name com.google.android.maps.v2.API_KEY. This legacy name allows authentication to the Android Maps API v2 only. An application can specify only one of the API key metadata names. If both are specified, the API throws an exception.

  10. In Android Studio, open your module-level build.gradle or build.gradle.kts file and edit the secrets property. If the secrets property does not exist, add it.

    Edit the properties of the plugin to set propertiesFileName to secrets.properties, set defaultPropertiesFileName to local.defaults.properties, and set any other properties.

    Groovy

    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.*"
    }
            

अपने ऐप्लिकेशन में ज़रूरी एट्रिब्यूशन शामिल करें

अगर आपके ऐप्लिकेशन में Android के लिए नेविगेशन SDK टूल का इस्तेमाल किया जाता है, तो आपको अपने ऐप्लिकेशन के कानूनी नोटिस वाले सेक्शन में, एट्रिब्यूशन टेक्स्ट और ओपन सोर्स लाइसेंस शामिल करने होंगे.

आपको ज़रूरी एट्रिब्यूशन टेक्स्ट और ओपन सोर्स लाइसेंस, Android zip फ़ाइल के लिए नेविगेशन SDK टूल में मिल सकते हैं:

  • NOTICE.txt
  • LICENSES.txt