ตั้งค่าโปรเจ็กต์ Android Studio

หน้านี้จะอธิบายวิธีกำหนดค่าโปรเจ็กต์ Android Studio ให้ใช้ Maps SDK สำหรับ Android โดยไม่ต้องใช้เทมเพลต Google Maps ซึ่งมีรายละเอียดอยู่ในคู่มือเริ่มใช้งานฉบับย่อ

เทมเพลต Google Maps จะกำหนดค่าและเพิ่มแผนที่พื้นฐานลงในโปรเจ็กต์ Android Studio ใหม่โดยอัตโนมัติ อย่างไรก็ตาม คุณสามารถเพิ่มแผนที่ในโครงการ Android ที่ใช้เทมเพลต Android Studio อื่นได้ด้วย ในการดำเนินการดังกล่าว คุณต้องกำหนดค่า โปรเจ็กต์ด้วยตนเอง แล้วเพิ่มแผนที่

ขั้นตอนที่ 1: ตั้งค่า Android Studio

  1. ต้องระบุ Android Studio Arctic Fox ขึ้นไป ดาวน์โหลดและติดตั้ง หากยังไม่ได้ดำเนินการ
  2. ตรวจสอบว่าคุณใช้ ปลั๊กอิน Android Gradle เวอร์ชัน 7.0 ขึ้นไปใน Android Studio

ขั้นตอนที่ 2 ตั้งค่า SDK

คลัง Maps SDK สำหรับ Android มีให้บริการผ่าน ที่เก็บ Maven ของ Google หากต้องการเพิ่ม SDK ลงในแอป ให้ทำดังนี้

  1. ในไฟล์ settings.gradle ระดับบนสุด ให้ใส่พอร์ทัลปลั๊กอิน Gradle, ที่เก็บ Google Maven และที่เก็บส่วนกลาง Maven ไว้ในบล็อก pluginManagement การบล็อก pluginManagement ต้องปรากฏก่อนคำสั่งอื่นๆ ในสคริปต์
    pluginManagement {
        repositories {
            gradlePluginPortal()
            google()
            mavenCentral()
        }
    } 
  2. ในไฟล์ settings.gradle ระดับบนสุด ให้ใส่ที่เก็บ Maven ของ Google และที่เก็บ Maven Central ไว้ในบล็อก dependencyResolutionManagement ดังนี้
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
        }
    } 
  3. ในไฟล์ build.gradle ระดับโมดูล ให้เพิ่มทรัพยากร Dependency ของบริการ Google Play สำหรับ Maps SDK สำหรับ Android
    dependencies {
    
        // Maps SDK for Android
        implementation 'com.google.android.gms:play-services-maps:18.2.0'
    } 
  4. ในไฟล์ build.gradle ระดับโมดูล ให้ตั้งค่า compileSdk และ minSdk เป็นค่าต่อไปนี้
    android {
        compileSdk 31
    
        defaultConfig {
            minSdk 19
            // ...
        }

ขั้นตอนที่ 3: เพิ่มคีย์ API ลงในโปรเจ็กต์

ส่วนนี้อธิบายวิธีจัดเก็บคีย์ API เพื่อให้แอปอ้างอิงได้อย่างปลอดภัย คุณไม่ควรตรวจสอบคีย์ API ในระบบควบคุมเวอร์ชัน เราจึงขอแนะนำให้เก็บคีย์นี้ไว้ในไฟล์ secrets.properties ซึ่งอยู่ในไดเรกทอรีรากของโปรเจ็กต์ ดูข้อมูลเพิ่มเติมเกี่ยวกับไฟล์ secrets.properties ได้ที่ไฟล์คุณสมบัติ Gradle

เราขอแนะนำให้ใช้ปลั๊กอินข้อมูลลับ Gradle สำหรับ Android เพื่อปรับปรุงงานนี้ให้มีประสิทธิภาพยิ่งขึ้น

วิธีติดตั้งปลั๊กอิน Secrets Gradle สำหรับ Android ในโปรเจ็กต์ Google Maps มีดังนี้

  1. ใน Android Studio ให้เปิดไฟล์ build.gradle ระดับโปรเจ็กต์และเพิ่มโค้ดต่อไปนี้ลงในองค์ประกอบ 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. Open your module-level build.gradle file and add the following code to the plugins element.

    Groovy

    plugins {
        // ...
        id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    }

    Kotlin

    plugins {
        id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
    }
  3. Save the file and sync your project with Gradle.
  4. Open the secrets.properties in your project level directory, and then add the following code. Replace YOUR_API_KEY with your API key.
    MAPS_API_KEY=YOUR_API_KEY