إعداد مشروع "استوديو Android"

تتوفّر حزمة تطوير البرامج (SDK) للتنقّل عبر Maven. بعد عند إنشاء مشروع التطوير، يمكنك دمج حزمة SDK به باستخدام باستخدام أحد الأساليب التالية.

فيما يلي استخدام مستودع Maven google()، وهو أبسط الموصى بها لإضافة حزمة SDK للتنقل إلى مشروعك.

  1. أضِف التبعية التالية إلى إعدادات Gradle أو Maven، استبدال العنصر النائب VERSION_NUMBER بـ الإصدار المطلوب من حزمة تطوير البرامج (SDK) للتنقل لنظام التشغيل Android.

    Gradle

    أضِف ما يلي إلى build.gradle على مستوى الوحدة:

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

    Maven

    أضِف ما يلي إلى pom.xml:

    <dependencies>
      ...
      <dependency>
        <groupId>com.google.android.libraries.navigation</groupId>
        <artifactId>navigation</artifactId>
        <version>VERSION_NUMBER</version>
      </dependency>
    </dependencies>
    
  2. إذا كانت لديك أي تبعيات تستخدم حزمة SDK لخرائط Google، فيجب عليك استبعاد في كل اعتمادية معلَن عنها تعتمد على حزمة تطوير البرامج بالاستناد إلى بيانات "خرائط Google".

    Gradle

    أضِف ما يلي إلى 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>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 للتنقل واستخدامها.

تعديل المواقع المحلية

  • في مجلد Gradle Scripts، افتح ملف local.properties وأضف android.useDeprecatedNdk=true

تعديل النص البرمجي لإنشاء Gradle

  • افتح ملف build.gradle (Module:app) واتّبِع الإرشادات التالية. تحديث الإعدادات لاستيفاء متطلبات حزمة SDK للتنقل ومراعاة إعداد وخيارات التحسين أيضًا.

    الإعدادات المطلوبة لحزمة تطوير البرامج (SDK) الخاصة بالتنقّل

    1. اضبط السمة minSdkVersion على 23 أو أعلى.
    2. اضبط السمة targetSdkVersion على 33 أو أعلى.
    3. أضِف إعداد dexOptions يزيد من قيمة javaMaxHeapSize.
    4. يمكنك ضبط مكان المكتبات الإضافية.
    5. إضافة repositories وdependencies حزمة SDK للتنقّل
    6. استبدال أرقام الإصدارات في العناصر التابعة والإصدارات المتوفرة.

    إعدادات اختيارية لتقليل وقت الإصدار

    • تمكين تقليص التعليمات البرمجية والموارد تقل استخدام R8/ProGuard لإزالة التعليمات البرمجية والموارد غير المستخدمة من التبعيات. في حال حذف يستغرق تنفيذ خطوة R8/ProGuard الكثير من الوقت، فكر في تمكين multidex لـ وأعمال التطوير.
    • تقليل عدد ترجمات اللغات المضمَّنة في الإصدار: ضبط resConfigs للغة واحدة خلال مرحلة التطوير. بالنسبة للبناء النهائي، يمكنك ضبط resConfigs للغات التي تستخدمها. بشكل تلقائي، تتضمن سلاسل موارد لكل اللغات المتاحة على حزمة SDK للتنقّل

    إضافة إزالة الألوان للتوافق مع Java8

    • إذا كنت تنشئ تطبيقك باستخدام الإصدار 4.0.0 من المكوّن الإضافي لنظام Gradle المتوافق مع Android أو لفترة أعلى، يوسّع المكون الإضافي دعم استخدام عدد من لغات Java 8 واجهات برمجة التطبيقات. راجع تحليل Java 8 الدعم لمزيد من المعلومات. انظر المثال حول إنشاء مقتطف النص البرمجي أدناه لمعرفة كيف خيارات التجميع والتبعية.

فيما يلي مثال على النص البرمجي لإصدار Gradle للتطبيق. اطّلِع على تطبيقات نموذجية للمجموعات المحدَّثة من التبعيات، كإصدار قد تكون حزمة SDK للتنقل التي تستخدمها متقدمة قليلاً أو وراء هذه الوثائق.

apply plugin: 'com.android.application'

ext {
    navSdk = "__NAVSDK_VERSION__"
}

android {
    compileSdk 33
    buildToolsVersion='28.0.3'

    defaultConfig {
        applicationId "<your id>"
        // Navigation SDK supports SDK 23 and later.
        minSdkVersion 23
        targetSdkVersion 33
        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 {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

repositories {
    // Navigation SDK for Android and other libraries are hosted on Google's Maven repository.
    google()
}

dependencies {
    // Include the Google Navigation SDK.
    // Note: remember to exclude Google Play service Maps SDK from your transitive
    // dependencies to avoid duplicate copies of the Google Maps SDK.
    api "com.google.android.libraries.navigation:navigation:${navSdk}"

    // Declare other dependencies for your app here.

    annotationProcessor "androidx.annotation:annotation:1.7.0"
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.9'
}

إضافة مفتاح واجهة برمجة التطبيقات إلى تطبيقك

يوضّح هذا القسم كيفية تخزين مفتاح واجهة برمجة التطبيقات كي يمكن الرجوع إليه بشكل آمن من خلال تطبيقك. يجب عدم التحقق من مفتاح واجهة برمجة التطبيقات في نظام التحكم في الإصدار، لذا ننصحك تخزينه في ملف secrets.properties، الموجود في الدليل الجذري مشروعك. لمزيد من المعلومات عن ملف secrets.properties، يمكنك الاطّلاع على ملفات خصائص Gradle:

لتبسيط هذه المهمة، نوصيك باستخدام المكوّن الإضافي السري لنظام Gradle المتوافق مع Android

لتثبيت مكوّن Secrets Gradle الإضافي لنظام التشغيل Android في مشروعك على "خرائط Google"، اتّبِع الخطوات التالية:

  1. في "استوديو Android"، افتح build.gradle أو build.gradle.kts المستوى الأعلى. وإضافة التعليمة البرمجية التالية إلى العنصر dependencies ضمن buildscript

    Groovy

    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

    Groovy

    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 من التحقق من عنصر تحكّم الإصدار .
    NAV_API_KEY=YOUR_API_KEY
  6. احفظ الملف.
  7. أنشئ الملف local.defaults.properties في دليل المستوى الأعلى، بالطريقة نفسها كملف secrets.properties، ثم أضف الرمز التالي.

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

إدراج عمليات تحديد المصدر المطلوبة في تطبيقك

إذا كنت تستخدم حزمة تطوير البرامج (SDK) للتنقل لنظام التشغيل Android في تطبيقك، يجب تضمين نص تحديد المصدر وتراخيص البرامج المفتوحة المصدر كجزء من الإشعارات القانونية في تطبيقك .

يمكنك العثور على نص المصدر المطلوب وتراخيص البرامج المفتوحة المصدر في حزمة SDK للتنقل لملف Android المضغوط:

  • NOTICE.txt
  • LICENSES.txt

إذا كنت أحد عملاء نقل المركبات أو خدمات توصيل مركبات الأسطول

إذا كنت أحد عملاء الجوّال أو Fleet Engine Delivery، تعرَّف على مزيد من المعلومات الفوترة في وثائق التنقل. لمزيد من المعلومات عن تسجيل المعاملات، راجع إعداد الفوترة، تسجيل المعاملات القابلة للفوترة إعداد التقارير تسجيل المعاملات القابلة للفوترة (Android)