버전 제어 시스템에 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요소에 추가합니다.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.google.android.libraries.mapsplatform.secrets-gradle-plugin' }Kotlin
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.gradleorbuild.gradle.ktsfile and edit thesecretsproperty. If thesecretsproperty does not exist, add it.Edit the properties of the plugin to set
propertiesFileNametosecrets.properties, setdefaultPropertiesFileNameto the name of your local properties file (eitherlocal.propertiesorlocal.defaults.propertiesdepending on how you created the project), and set any other properties.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.*" }다음 단계
- Android용 Secrets Gradle Plugin GitHub 프로젝트 페이지를 확인합니다.
- 플러그인 사용의 전체 예는 Android 스튜디오 프로젝트 설정을 참고하세요.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2024-01-22(UTC)
[null,null,["최종 업데이트: 2024-01-22(UTC)"],[],["To protect API keys, store them in a `secrets.properties` file, excluded from version control. Install the Secrets Gradle Plugin by adding it to the `build.gradle` files. In module-level `build.gradle`, configure the plugin to read from `secrets.properties` and set `local.defaults.properties` as backup. Add your API key to `secrets.properties` and use the `DEFAULT_API_KEY` in the backup file. Sync the project to apply changes. This plugin exposes keys as variables.\n"]]