Android용 Places SDK를 사용하도록 앱을 구성하려면 다음 단계를 따르세요. Android용 Places SDK를 사용하는 모든 앱에 필요합니다.
1단계: Android 스튜디오 설정하기
이 문서에서는 Android 스튜디오 Hedgehog 및 Android Gradle 플러그인 버전 8.2를 사용하는 개발 환경을 설명합니다.
2단계: SDK 설정
Places SDK for Android 라이브러리는 Google의 Maven 저장소를 통해 사용할 수 있습니다. 앱에 SDK를 추가하려면 다음 지침을 따르세요.
- 최상위 수준
settings.gradle.kts
파일의pluginManagement
블록 아래에 Gradle 플러그인 포털, Google Maven 저장소, Maven 중앙 저장소를 포함합니다.pluginManagement
블록은 스크립트의 모든 문 앞에 표시되어야 합니다.pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() } }
- 최상위 수준
settings.gradle.kts
파일의dependencyResolutionManagement
블록 아래 Google Maven 저장소, Maven 중앙 저장소를 포함합니다.dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } }
-
모듈 수준
build.gradle.kts
파일의dependencies
섹션에 Android용 Places SDK 종속 항목을 추가합니다.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:4.3.1") }
- 모듈 수준
build.gradle.kts
파일에서compileSdk
및minSdk
를 다음 값으로 설정합니다.Groovy
android { compileSdk 34 defaultConfig { minSdk 23 // ... } }
Kotlin
android { compileSdk = 34 defaultConfig { minSdk = 23 // ... } }
- 모듈 수준
build.gradle
파일의buildFeatures
섹션에서 이 절차의 뒷부분에 정의된 메타데이터 값에 액세스하는 데 사용하는BuildConfig
클래스를 추가합니다.Groovy
android { // ... buildFeatures { buildConfig true // ... } }
Kotlin
android { // ... buildFeatures { buildConfig = true // ... } }
3단계: 프로젝트에 API 키 추가
이 섹션에서는 API 키를 앱이 더욱 안전하게 참조할 수 있도록 저장하는 방법을 설명합니다. API 키는 버전 제어 시스템에 체크인하면 안 되기 때문에 프로젝트의 루트 디렉터리에 있는 secrets.properties
파일에 저장할 것을 권장합니다. secrets.properties
파일에 관한 자세한 내용은
Gradle 속성 파일을 참고하세요.
이 작업을 간소화하려면 Android용 Secrets Gradle 플러그인을 사용하는 것이 좋습니다.
Google 지도 프로젝트에 Android용 Secrets Gradle 플러그인을 설치하려면 다음 단계를 따르세요.
-
Android 스튜디오에서 최상위 수준
build.gradle.kts
또는build.gradle
파일을 열고 다음 코드를buildscript
아래dependencies
요소에 추가합니다.Kotlin
buildscript { dependencies { classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1") } }
Groovy
buildscript { dependencies { classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1" } }
-
모듈 수준
build.gradle.kts
또는build.gradle
파일을 열고plugins
요소에 다음 코드를 추가합니다.Kotlin
plugins { // ... id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
Groovy
plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' }
- 모듈 수준
build.gradle.kts
또는build.gradle
파일에서targetSdk
및compileSdk
가 34로 설정되어 있는지 확인합니다. - Gradle과 프로젝트를 동기화합니다.
-
최상위 수준 디렉터리에서
secrets.properties
파일을 연 후 다음 코드를 추가합니다.YOUR_API_KEY
를 직접 생성한 API 키로 변경합니다.secrets.properties
가 버전 제어 시스템에 체크인되는 데서 제외되었으므로 키를 이 파일에 저장합니다.PLACES_API_KEY=YOUR_API_KEY
-
최상위 수준 디렉터리에서
secrets.properties
파일과 동일한 폴더에local.defaults.properties
파일을 만든 후 다음 코드를 추가합니다.PLACES_API_KEY=DEFAULT_API_KEY
이 파일의 목적은
secrets.properties
파일이 없는 경우 빌드에 실패하지 않도록 API 키의 백업 위치를 제공하는 것입니다. 이는 버전 제어 시스템에서secrets.properties
가 빠진 앱을 복제하거나 API 키를 제공하는secrets.properties
파일을 아직 로컬에서 생성하지 않은 경우 발생할 수 있습니다. -
Android 스튜디오에서 모듈 수준
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 same folder as the 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 same folder as the 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 클라이언트 초기화
활동 또는 프래그먼트 내에서 Android용 Places SDK를 초기화합니다. 먼저 사용할 SDK 버전을 결정해야 합니다. Android용 Places SDK 또는 Android용 Places SDK (신규) 중 하나를 선택하세요. 제품 버전에 관한 자세한 내용은 SDK 버전 선택하기를 참고하세요.
다음 예에서는 두 버전의 SDK를 초기화하는 방법을 보여줍니다.Android용 Places SDK (신규)
Places.initializeWithNewPlacesApiEnabled()
을 호출할 때 API 키를 전달합니다.
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)
자바
// 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);
Android용 Places SDK
Places.initializeWithNewPlacesApiEnabled()
을 호출할 때 API 키를 전달합니다.
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)
자바
// 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);
이제 Android용 Places SDK를 사용할 수 있습니다.
5단계: Android 기기 설정하기
Android용 Places SDK를 사용하는 앱을 실행하려면 Google API를 포함하는 Android 5.0 이상의 Android 기기 또는 에뮬레이터에 Android용 Places SDK를 배포해야 합니다.
- Android 기기를 사용하려면 하드웨어 기기에서 앱 실행의 안내를 따르세요.
- Android Emulator를 사용하려면 Android 스튜디오와 함께 제공되는 Android Virtual Device(AVD) Manager를 이용해 가상 기기를 만들고 에뮬레이터를 설치하면 됩니다.