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:5.0.0") }
- 모듈 수준
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 플러그인을 사용하는 것이 좋습니다.
Android용 Secrets Gradle 플러그인을 설치하고 API 키를 저장하려면 다음 단계를 따르세요.
-
Android 스튜디오에서 루트 수준
build.gradle파일을 열고 다음 코드를buildscript아래dependencies요소에 추가합니다.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") } }
-
앱 수준
build.gradle파일을 열고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") }
- Android 스튜디오를 사용하는 경우 Gradle과 프로젝트를 동기화합니다.
-
프로젝트 수준 디렉터리에서
local.properties를 열고 다음 코드를 추가합니다.YOUR_API_KEY를 직접 생성한 API 키로 변경합니다.PLACES_API_KEY=YOUR_API_KEY
4단계: Places API 클라이언트 초기화
활동 또는 프래그먼트 내에서 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를 사용할 수 있습니다.
5단계: Android 기기 설정하기
Android용 Places SDK를 사용하는 앱을 실행하려면 Google API를 포함하는 Android 5.0 이상의 Android 기기 또는 에뮬레이터에 Android용 Places SDK를 배포해야 합니다.
- Android 기기를 사용하려면 하드웨어 기기에서 앱 실행의 안내를 따르세요.
- Android Emulator를 사용하려면 Android 스튜디오와 함께 제공되는 Android Virtual Device(AVD) Manager를 이용해 가상 기기를 만들고 에뮬레이터를 설치하면 됩니다.