Để định cấu hình ứng dụng nhằm dùng SDK Địa điểm dành cho Android, hãy làm theo các bước sau. Đây là yêu cầu bắt buộc đối với tất cả các ứng dụng sử dụng SDK Địa điểm dành cho Android.
Bước 1: Thiết lập Android Studio
Tài liệu này mô tả môi trường phát triển sử dụ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 SDK Địa điểm dành 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
cấp cao nhất, hãy đưa cổng thông tin trình bổ trợ Gradle, Kho lưu trữ Google Maven và Kho lưu trữ trung tâm Maven vào khốipluginManagement
. KhốipluginManagement
phả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
cấp cao nhất, hãy đưa Kho lưu trữ Maven của Google và Kho lưu trữ trung tâm Maven vào khốidependencyResolutionManagement
:dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } }
-
Trong mục
dependencies
của tệpbuild.gradle
ở cấp mô-đun, hãy thêm một phần phụ thuộc vào SDK Địa điểm dành cho Android:Groovy
dependencies { // If updating kotlin-bom version number above, also edit project-level build.gradle definition of $kotlin_version variable implementation(platform("org.jetbrains.kotlin:kotlin-bom:$kotlin_version")) implementation 'com.google.android.libraries.places:places:3.3.0' }
Kotlin
dependencies { implementation(platform("org.jetbrains.kotlin:kotlin-bom:$kotlin_version")) implementation("com.google.android.libraries.places:places:3.5.0") }
- Trong tệp
build.gradle
ở cấp mô-đun, hãy đặtcompileSdk
vàminSdk
thành các giá trị sau:Groovy
android { compileSdk 34 defaultConfig { minSdk 21 // ... } }
Kotlin
android { compileSdk = 34 defaultConfig { minSdk = 21 // ... } }
- Trong phần
buildFeatures
của tệpbuild.gradle
cấp mô-đun, hãy thêm lớpBuildConfig
mà 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 quản lý phiên bản. Vì vậy, bạn nên lưu trữ khoá đó 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 các 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 trong dự án Google Maps:
-
Trong Android Studio, hãy mở tệp
build.gradle
hoặcbuild.gradle.kts
ở cấp cao nhất rồi thêm mã sau vào phần tửdependencies
trongbuildscript
.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 mô-đun và thêm mã sau vào phần tửplugins
.Groovy
plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
Kotlin
plugins { id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
- Trong tệp
build.gradle
ở cấp mô-đun, hãy đảm bảo rằngtargetSdk
vàcompileSdk
được đặt thành 34. - Lưu tệp và đồng bộ hoá dự án của bạn với Gradle.
-
Mở tệp
secrets.properties
trong thư mục cấp cao nhất của bạn, sau đó thêm mã sau. Thay thếYOUR_API_KEY
bằng khoá API. Lưu trữ khoá của bạn trong tệp này vìsecrets.properties
sẽ không được kiểm tra trong một hệ thống quản lý phiên bản.PLACES_API_KEY=YOUR_API_KEY
- Lưu tệp.
-
Tạo tệp
local.defaults.properties
trong thư mục cấp cao nhất, cùng thư mục với tệpsecrets.properties
, sau đó thêm mã sau.PLACES_API_KEY=DEFAULT_API_KEY
Mục đích của tệp này là cung cấp vị trí sao lưu cho khoá API nếu hệ thống không tìm thấy tệp
secrets.properties
để các bản dựng sẽ không bị lỗi. Điều này có thể xảy ra nếu bạn sao chép ứng dụng từ một hệ thống quản lý phiên bản bỏ quasecrets.properties
và bạn chưa tạo tệpsecrets.properties
cục bộ để cung cấp khoá API. - Lưu tệp.
-
Trong Android Studio, hãy mở tệp
build.gradle
hoặcbuild.gradle.kts
ở cấp mô-đun rồi chỉnh sửa thuộc tínhsecrets
. Nếu thuộc tínhsecrets
không tồn tại, hãy thêm thuộc tính đó.Chỉnh sửa các thuộc tính của trình bổ trợ để đặt
propertiesFileName
thànhsecrets.properties
, đặtdefaultPropertiesFileName
thànhlocal.defaults.properties
và đặt mọi thuộc tính khác.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.*" }
Bước 4. Khởi chạy ứng dụng API Địa điểm
Khởi chạy SDK địa điểm dành cho Android trong một hoạt động hoặc mảnh. Trước tiên, bạn phải quyết định phiên bản SDK sẽ sử dụng: SDK Địa điểm dành cho Android hoặc SDK Địa điểm dành cho Android (Mới). Để biết thêm thông tin về các phiên bản sản phẩm, hãy xem phần Chọn phiên bản SDK.
Ví dụ sau đây cho thấy cách khởi chạy SDK cho cả hai phiên bản.SDK Địa điểm dành cho Android (Mới)
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);
SDK Địa điểm dành cho Android
Truyền khoá API khi gọi Places.initialize()
:
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.initialize(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.initialize(getApplicationContext(), apiKey); // Create a new PlacesClient instance PlacesClient placesClient = Places.createClient(this);
Bây giờ, bạn đã sẵn sàng bắt đầu sử dụng SDK địa điểm dành cho Android!
Bước 5: Thiết lập thiết bị Android
Để chạy một ứng dụng có sử dụng SDK Địa điểm dành cho Android, bạn phải triển khai ứng dụng đó trên thiết bị Android hoặc trình mô phỏng Android dựa trên Android 5.0 trở lên và bao gồm các API của Google.
- Để sử dụng thiết bị Android, hãy làm theo hướng dẫn trong bài viết 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 cách sử dụng Trình quản lý thiết bị Android ảo (AVD) đi kèm với Android Studio.