버전 제어 시스템에 API 키를 체크인하지 않는 것이 좋습니다. 대신 프로젝트의 루트 디렉터리에 있지만
버전 제어에서 제외된 로컬 secrets.properties
파일에 저장한 후
Android용 Secrets Gradle 플러그인을 사용하여
API 키를 읽어야 합니다.
Android용 Secrets Gradle 플러그인은 버전 제어 시스템에 체크인하지 않은 속성 파일에서 API 키를 포함한 보안 비밀을 읽습니다. 그런 다음 이 속성을 Gradle에서 생성된 BuildConfig
클래스와 Android 매니페스트 파일에 변수로 노출합니다.
Android용 Secrets Gradle 플러그인을 사용하여 API 키에 액세스하는 전체 예는 Android 스튜디오 프로젝트 설정을 참고하세요.
설치 및 사용
Google 지도 프로젝트에 Android용 Secrets Gradle 플러그인을 설치하려면 다음 단계를 따르세요.
-
Android 스튜디오에서 최상위 수준
build.gradle
또는build.gradle.kts
파일을 열고 다음 코드를buildscript
아래dependencies
요소에 추가합니다.buildscript {
dependencies {
classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1"
}
}buildscript {
dependencies {
classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1")
}
} -
모듈 수준
build.gradle
파일을 열고plugins
요소에 다음 코드를 추가합니다.plugins {
// ...
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
}plugins {
id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
} - 모듈 수준
build.gradle
파일에서targetSdk
및compileSdk
를 34로 설정해야 합니다. - 파일을 저장하고 프로젝트를 Gradle과 동기화합니다.
-
최상위 수준 디렉터리에서
secrets.properties
파일을 연 후 다음 코드를 추가합니다.YOUR_API_KEY
를 API 키로 변경합니다.MAPS_API_KEY=
YOUR_API_KEY - 파일을 저장합니다.
-
AndroidManifest.xml
파일에서com.google.android.geo.API_KEY
로 이동한 후android:value attribute
를 업데이트합니다.<meta-data>
태그가 존재하지 않으면<application>
태그의 하위 요소로 태그를 만듭니다.<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${MAPS_API_KEY}" /> -
In Android Studio, open your module-level
build.gradle
orbuild.gradle.kts
file and edit thesecrets
property. If thesecrets
property does not exist, add it.Edit the properties of the plugin to set
propertiesFileName
tosecrets.properties
, setdefaultPropertiesFileName
to the name of your local properties file (eitherlocal.properties
orlocal.defaults.properties
depending on how you created the project), and set any other properties.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.*"
}
다음 단계
- Android용 Secrets Gradle Plugin GitHub 프로젝트 페이지를 확인합니다.
- 플러그인 사용의 전체 예는 Android 스튜디오 프로젝트 설정을 참고하세요.