הגדרת הפרויקט – גרסה 4.99 וגרסאות קודמות

המדריך הזה מפרט את הדרישות לגבי תצורת build לשימוש ב-Navigation SDK ל-Android. ההנחיות מבוססות על ההנחה שיש לכם סביבת פיתוח משולבת (IDE) של Android, שאתם מכירים את הפיתוח של Android.

דרישות מינימליות לשימוש ב-Navigation SDK

הדרישות האלה חלות על Navigation SDK ל-Android בגרסה 4.99 ובגרסאות קודמות.

  • פרויקט במסוף Google Cloud ש-Navigation SDK מופעל בו. להקצאת הרשאות ידנית, צריך לפנות לנציג שלכם בפלטפורמה של מפות Google.

  • האפליקציה חייבת לטרגט לרמת API 30 ומעלה.

  • כדי להריץ אפליקציה שנוצרה עם SDK לניווט, צריך להתקין במכשיר ה-Android את Google Play Services ולהפעיל אותו.

  • חובה להוסיף לאפליקציה טקסטים של שיוך ורישוי.

הגדרת הפרויקטים: פרויקט במסוף Cloud ופרויקט Android

לפני שתוכלו לפתח או לבדוק אפליקציה, אתם צריכים ליצור פרויקט במסוף Cloud ולהוסיף פרטי כניסה של מפתח API. כדי לגשת ל-Navigation SDK, צריך להקצות לפרויקט. כל המפתחות בפרויקט במסוף Cloud מקבלים את אותה גישה ל-Navigation SDK. למפתח יכול להיות משויך יותר מפרויקט פיתוח אחד. אם כבר יש לכם פרויקט במסוף, אתם יכולים להוסיף מפתח לפרויקט הנוכחי.

כדי להגדיר

  1. בדפדפן האינטרנט המועדף, נכנסים למסוף Cloud ויוצרים את הפרויקט במסוף Cloud.
  2. בסביבת IDE, למשל Android Studio, יוצרים פרויקט פיתוח אפליקציה ל-Android ורושמים את שם החבילה.
  3. כדי לתת גישה ל-Navigation SDK בפרויקט שלכם במסוף Cloud, עליכם לפנות לנציג שלכם בפלטפורמה של מפות Google.
  4. במרכז הבקרה של Cloud Console בדפדפן האינטרנט, יוצרים פרטי כניסה כדי ליצור מפתח API עם הגבלות.
  5. ב-API key page, לוחצים על apps ל-Android באזור Application restrictions.
  6. לוחצים על Add את שם החבילה ואת טביעת האצבע, ואז מזינים את שם החבילה של פרויקט הפיתוח ואת טביעת האצבע SHA-1 של המפתח.
  7. לוחצים על שמירה.

הוספת SDK לניווט לפרויקט

ה-Navigation SDK זמין דרך Maven או כחבילת AAR. אחרי שיוצרים פרויקט פיתוח, אפשר לשלב בו את ה-SDK באחת מהשיטות הבאות.

בדוגמה הבאה משתמשים במאגר google() Maven, וזו הדרך הפשוטה והמומלצת להוסיף את Navigation SDK לפרויקט

  1. מוסיפים את התלות הבאה להגדרות של Gradle או Maven, ומחליפים את ה-placeholder של VERSION_NUMBER בגרסה הרצויה של Navigation SDK ל-Android.

    Gradle

    מוסיפים את הפריטים הבאים ל-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>
    

    אם יש לכם יחסי תלות שמשתמשים ב-SDK של מפות Google, אתם צריכים להחריג את התלות בכל תלות מוצהרת שמסתמכת על ה-SDK של מפות Google.

    <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>
    

שימוש ב-Maven for Navigation SDK לפני גרסה 4.5, או עם Driver SDK

ה-Navigation SDK ימשיך להיות זמין דרך מאגר Maven המקורי עד לשאר הגרסאות של v4. זאת אותה ספרייה עם כל העדכונים כמו הגרסה שלמעלה, והיא מספקת תאימות ל-Driver SDK ולספריות אחרות במהלך המעבר. כדי להשתמש בתלות הזו, צריך להתחבר לפרויקט בענן דרך gcloud במהלך ההידור.

  1. מגדירים את הסביבה כדי לגשת למאגר Maven של Google, כפי שמתואר בקטע דרישות מוקדמות במאמרי העזרה לצרכנים בנושא SDK. הגישה ל-Navigation SDK נשלטת באמצעות קבוצה של סביבת עבודה.
  2. מוסיפים את יחסי התלות הבאים להגדרות של Gradle או Maven, ומחליפים את ה-placeholder הזה VERSION_NUMBER בגרסה הרצויה של Navigation SDK.

    Gradle

    מוסיפים את הפריטים הבאים ל-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>
    

    אם יש לכם יחסי תלות שמשתמשים ב-SDK של מפות Google, אתם צריכים להחריג את התלות בכל תלות מוצהרת שמסתמכת על ה-SDK של מפות Google.

    <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. ההוראות האלה מבוססות על ההנחה שהשימוש ב-Android Studio בסביבת הפיתוח המשולבת שלכם הוא.

  1. מורידים את הגרסה האחרונה של Navigation SDK מGoogle Drive המשותף ומחלצים את תוכן ה-ZIP. אם אין לכם גישה, תוכלו לפנות לנציג שלכם.

  2. ב-Android Studio, פותחים פרויקט ומוסיפים את חבילת Google Play Services באמצעות מנהל ה-SDK.

  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'
            }
        }
    }
    

הגדרת ה-build

אחרי שיוצרים את הפרויקט, אפשר לקבוע את ההגדרות כדי לבנות build מוצלח ולהשתמש ב-Navigation SDK.

עדכון מאפיינים מקומיים

  • בתיקייה Gradle Scripts, פותחים את הקובץ local.properties ומוסיפים את android.useDeprecatedNdk=true.

עדכון סקריפט ה-build של Gradle

  • פותחים את הקובץ build.gradle (Module:app) ומעדכנים את ההגדרות בהתאם לדרישות של Navigation SDK, וכדאי להגדיר גם את אפשרויות האופטימיזציה.

    הגדרות נדרשות ל-Navigation SDK

    1. מגדירים את הערך של minSdkVersion לערך 23 ומעלה.
    2. מגדירים את הערך של targetSdkVersion לערך 30 ומעלה.
    3. צריך להוסיף הגדרת dexOptions שמגדילה את הערך של javaMaxHeapSize.
    4. הגדרת המיקום לספריות נוספות.
    5. מוסיפים את repositories ואת dependencies של ה-SDK לניווט.
    6. מחליפים את מספרי הגרסאות של יחסי התלות בגרסאות האחרונות שזמינות.

    הגדרות אופציונליות לקיצור זמן ה-build

    • מפעילים כיווץ קוד וכיווץ משאבים באמצעות R8/ProGuard כדי להסיר מיחסי התלות קוד ומשאבים שלא נמצאים בשימוש. אם ההרצה של השלב R8/ProGuard נמשכת יותר מדי זמן, כדאי להפעיל multidex לצורכי פיתוח.
    • צריך לצמצם את מספר התרגומים של השפות שנכללים ב-build: צריך להגדיר את resConfigs לשפה אחת במהלך הפיתוח. לגרסת ה-build הסופית, מגדירים resConfigs לשפות בפועל. כברירת מחדל, Gradle כוללת מחרוזות משאבים לכל השפות שנתמכות ב-Navigation SDK.

בהמשך מוצגת דוגמה לסקריפט ה-build של Gradle לאפליקציה. כדאי לבדוק את האפליקציות לדוגמה כדי לראות אם קבוצות יחסי התלות מעודכנות, כי הגרסה של Navigation 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'
}

הוספה של מפתח ה-API לאפליקציה

בקטע הזה נסביר איך לאחסן את מפתח ה-API כדי שהאפליקציה תוכל להפנות אליו באופן מאובטח. אסור לבדוק את מפתח ה-API במערכת לניהול גרסאות. לכן מומלץ לשמור אותו בקובץ secrets.properties, שנמצא בתיקיית השורש של הפרויקט. למידע נוסף על הקובץ secrets.properties ראו קובצי מאפייני Gradle.

כדי לייעל את המשימה הזו, מומלץ להשתמש בפלאגין של Secrets Gradle ל-Android.

כדי להתקין את הפלאגין Secrets Gradle ל-Android בפרויקט במפות Google:

  1. ב-Android Studio, פותחים את הקובץ build.gradle או build.gradle.kts ברמה העליונה, ומוסיפים את הקוד הבא לרכיב dependencies בקטע buildscript.

    מגניב

    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 במפתח ה-API שלכם. אפשר לאחסן את המפתח בקובץ הזה כי הבדיקה של secrets.properties לא נכללת במערכת לניהול גרסאות.
    NAV_API_KEY=YOUR_API_KEY
  6. שומרים את הקובץ.
  7. יוצרים את הקובץ local.defaults.properties בספרייה שברמה העליונה, את אותה התיקייה שבה נמצא הקובץ secrets.properties, ואז מוסיפים את הקוד הבא.

    NAV_API_KEY=DEFAULT_API_KEY

    מטרת הקובץ הזה היא לספק מיקום לגיבוי של מפתח ה-API אם הקובץ secrets.properties לא נמצא, כדי שגרסאות ה-build לא ייכשלו. מצב כזה יכול לקרות אם משכפלים את האפליקציה ממערכת לניהול גרסאות שכתוצאה מכך להשמיט את secrets.properties ועדיין לא יצרתם קובץ secrets.properties באופן מקומי כדי לספק את מפתח ה-API שלכם.

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

לכלול את הייחוסים הנדרשים באפליקציה

אם משתמשים ב-Navigation SDK ל-Android באפליקציה, צריך לכלול טקסט שיוך ורישיונות קוד פתוח כחלק מהקטע 'הודעות משפטיות' של האפליקציה.

ניתן למצוא את טקסט הייחוס הנדרש ואת רישיונות הקוד הפתוח בקובץ ה-zip של Navigation SDK ל-Android:

  • NOTICE.txt
  • LICENSES.txt