Để định cấu hình ứng dụng sử dụng Places SDK cho Android, hãy làm theo các bước sau. Đây là những yêu cầu bắt buộc đối với tất cả ứng dụng sử dụng Places SDK cho Android.
Bước 1: Thiết lập Android Studio
Tài liệu này mô tả một môi trường phát triển bằng Android Studio Hedgehog và trình bổ trợ Android cho Gradle phiên bản 8.2.
Bước 2. Thiết lập SDK
Thư viện Places SDK cho Android có sẵn thông qua kho lưu trữ Maven của Google. Để thêm SDK vào ứng dụng, hãy làm như sau:
- Trong tệp
settings.gradle.ktscấp cao nhất, hãy thêm cổng trình bổ trợ Gradle, kho lưu trữ Google Maven và kho lưu trữ trung tâm Maven trong khốipluginManagement. KhốipluginManagementphải xuất hiện trước mọi câu lệnh khác trong tập lệnh.pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() } }
- Trong tệp
settings.gradle.ktscấp cao nhất, hãy thêm kho lưu trữ Maven của Google và kho lưu trữ trung tâm Maven trong khốidependencyResolutionManagement:dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } }
-
Trong phần
dependenciescủa tệpbuild.gradle.ktsở cấp mô-đun, hãy thêm một phần phụ thuộc vào Places SDK cho 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:5.0.0") }
- Trong tệp
build.gradle.ktsở cấp mô-đun, hãy đặtcompileSdkvàminSdkthành các giá trị sau:Groovy
android { compileSdk 34 defaultConfig { minSdk 23 // ... } }
Kotlin
android { compileSdk = 34 defaultConfig { minSdk = 23 // ... } }
- Trong phần
buildFeaturescủa tệpbuild.gradleở cấp mô-đun, hãy thêm lớpBuildConfigmà bạn dùng để truy cập vào các giá trị siêu dữ liệu được xác định sau trong quy trình này:Groovy
android { // ... buildFeatures { buildConfig true // ... } }
Kotlin
android { // ... buildFeatures { buildConfig = true // ... } }
Bước 3: Thêm khoá API vào dự án
Phần này mô tả cách lưu trữ khoá API để ứng dụng của bạn có thể tham chiếu một cách an toàn. Bạn không nên kiểm tra khoá API trong hệ thống kiểm soát phiên bản. Vì vậy, bạn nên lưu trữ khoá này trong tệp secrets.properties nằm trong thư mục gốc của dự án. Để biết thêm thông tin về tệp secrets.properties, hãy xem phần Tệp thuộc tính Gradle.
Để đơn giản hoá tác vụ này, bạn nên sử dụng Trình bổ trợ Secrets Gradle cho Android.
Cách cài đặt Trình bổ trợ Secrets Gradle cho Android và lưu trữ khoá API:
-
Trong Android Studio, hãy mở tệp
build.gradleở cấp gốc rồi thêm đoạn mã sau vào phần tửdependenciestrongbuildscript.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") } }
-
Mở tệp
build.gradleở cấp ứng dụng rồi thêm đoạn mã sau vào phần tửplugins.Groovy
plugins { id 'com.android.application' // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
Kotlin
plugins { id("com.android.application") // ... id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
- Nếu bạn sử dụng Android Studio, hãy đồng bộ hoá dự án với Gradle.
-
Mở
local.propertiestrong thư mục cấp dự án, sau đó thêm đoạn mã sau. Thay thếYOUR_API_KEYbằng khoá API của bạn.PLACES_API_KEY=YOUR_API_KEY
Bước 4. Khởi chạy ứng dụng Places API
Để khởi chạy Places SDK for Android trong một hoạt động hoặc mảnh, hãy truyền khoá API khi gọi 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);
Giờ đây, bạn đã sẵn sàng bắt đầu sử dụng Places SDK cho Android!
Bước 5: Thiết lập thiết bị Android
Để chạy một ứng dụng sử dụng Places SDK cho Android, bạn phải triển khai ứng dụng đó trên một thiết bị Android hoặc trình mô phỏng Android dựa trên Android 5.0 trở lên và có các API của Google.
- Để sử dụng thiết bị Android, hãy làm theo hướng dẫn trong phần Chạy ứng dụng trên thiết bị phần cứng.
- Để sử dụng trình mô phỏng Android, bạn có thể tạo một thiết bị ảo và cài đặt trình mô phỏng bằng Trình quản lý thiết bị ảo Android (AVD) đi kèm với Android Studio.