כדי להגדיר את האפליקציה לשימוש ב-Places SDK for Android, פועלים לפי השלבים הבאים. הם נדרשים לכל האפליקציות שמשתמשות ב-Places SDK ל-Android.
שלב 1: הגדרת Android Studio
במסמך הזה מתוארת סביבת פיתוח שמשתמשת ב-Android Studio Hedgehog ובפלאגין של Android Gradle בגרסה 8.2.
שלב 2. הגדרת ה-SDK
ספריית Places SDK ל-Android זמינה דרך מאגר Maven של Google. כדי להוסיף את ה-SDK לאפליקציה:
- בקובץ
settings.gradle.ktsברמה העליונה, צריך לכלול את פורטל הפלאגין של Gradle, את מאגר ה-Maven של Google ואת מאגר ה-Maven המרכזי בבלוקpluginManagement. הבלוקpluginManagementחייב להופיע לפני כל הצהרה אחרת בסקריפט.pluginManagement { repositories { google() mavenCentral() gradlePluginPortal() } }
- בקובץ
settings.gradle.ktsברמה העליונה, צריך לכלול את מאגר ה-Maven של Google ואת מאגר מרכזי של Maven בקטעdependencyResolutionManagement:dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS) repositories { google() mavenCentral() } }
-
בקטע
dependenciesבקובץbuild.gradle.ktsברמת המודול, מוסיפים תלות ב-Places SDK ל-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 // ... } }
- בקטע
buildFeaturesשל הקובץbuild.gradleברמת המודול, מוסיפים את המחלקהBuildConfig, שמשמשת לגישה לערכי המטא-נתונים שמוגדרים בהמשך התהליך:Groovy
android { // ... buildFeatures { buildConfig true // ... } }
Kotlin
android { // ... buildFeatures { buildConfig = true // ... } }
שלב 3: מוסיפים את מפתח ה-API לפרויקט
בקטע הזה מוסבר איך לאחסן את מפתח ה-API כדי שהאפליקציה תוכל להפנות אליו בצורה מאובטחת. לא כדאי להכניס את מפתח ה-API למערכת לניהול גרסאות, ולכן מומלץ לאחסן אותו בקובץ secrets.properties בתיקיית השורש של הפרויקט, ולהחריג את הקובץ הזה ממערכת לניהול גרסאות. מידע נוסף על קובצי מאפיינים של Gradle זמין במאמר קובצי מאפיינים של Gradle.
כדי לפשט את המשימה הזו, מומלץ להשתמש בפלאגין Secrets Gradle ל-Android.
כדי להתקין את Secrets Gradle Plugin for Android ולשמור את מפתח ה-API:
-
ב-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במפתח ה-API שלכם. חשוב להוסיף אתsecrets.propertiesלקובץ.gitignore(או להחריג אותו בדרך אחרת ממערכת ניהול הגרסאות) כדי לא להכניס את מפתח ה-API לניהול הגרסאות.PLACES_API_KEY=YOUR_API_KEY
-
יוצרים את הקובץ
local.defaults.propertiesבספרייה ברמה העליונה, באותה תיקייה שבה נמצא הקובץsecrets.properties, ואז מוסיפים את הקוד הבא.PLACES_API_KEY=DEFAULT_API_KEY
המטרה של הקובץ הזה היא לספק מיקום גיבוי למפתח ה-API אם הקובץ
secrets.propertiesלא נמצא, כדי שה-builds לא ייכשלו. זה יכול לקרות אם משכפלים את האפליקציה ממערכת בקרת גרסאות שמשמיטה אתsecrets.properties, ועדיין לא יוצרים קובץsecrets.propertiesבאופן מקומי כדי לספק את מפתח ה-API. -
ב-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" }
שלב 4. הפעלת הלקוח של Places API
כדי להפעיל את Places SDK ל-Android בתוך פעילות או מקטע, מעבירים את מפתח ה-API כשקוראים ל-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 ל-Android.
שלב 5: מגדירים מכשיר Android
כדי להפעיל אפליקציה שמשתמשת ב-Places SDK ל-Android, צריך לפרוס אותה במכשיר Android או באמולטור Android שמבוסס על Android מגרסה 6.0 ואילך וכולל את Google APIs.
- כדי להשתמש במכשיר Android, פועלים לפי ההוראות במאמר הפעלת אפליקציות במכשיר חומרה.
- כדי להשתמש באמולטור Android, אתם יכולים ליצור מכשיר וירטואלי ולהתקין את האמולטור באמצעות Device Manager שמגיע עם Android Studio.