๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Android] Secrets Gradle Plugin for Android

Google Map SDK๋ฅผ ์ถ”๊ฐ€ํ•˜๋‹ค๊ฐ€ ์•Œ๊ฒŒ๋œ ํ”Œ๋Ÿฌ๊ทธ์ธ์ด๋‹ค.

 

https://github.com/google/secrets-gradle-plugin

 

GitHub - google/secrets-gradle-plugin: A Gradle plugin for providing your secrets to your Android project.

A Gradle plugin for providing your secrets to your Android project. - google/secrets-gradle-plugin

github.com

 

 

API ํ‚ค๋ฅผ ์ˆจ๊ฒจ์ค€๋‹ค๊ณ  ํ•˜๋Š”๋ฐ, ์—ญ์‹œ๋‚˜ ์•ฑ ๋‚ด์— API ํ‚ค๋ฅผ ํ•˜๋“œ์ฝ”๋”ฉํ•˜์ง€ ์•Š๊ฒŒ ํ•ด์ค€๋‹ค๋Š” ์˜๋ฏธ์˜€๋‹ค. ๋ฒ„์ „ ์ปจํŠธ๋กค ์‹œ์˜ API ์ˆจ๊น€์ด๋ผ์„œ APK๋ฅผ ๋””์ปดํŒŒ์ผํ•˜๋ฉด API ํ‚ค๊ฐ€ ๋ณต์›๋œ๋‹ค๊ณ  Repository์˜ README์— ๋ถ„๋ช…ํžˆ ์„ค๋ช…๋˜์–ด ์žˆ๋‹ค. 

 

์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์„ธํŒ…

1. ์ผ๋‹จ BuildConfig๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ enable ์‹œ์ผœ์•ผ ํ•œ๋‹ค. 

app์˜ build.gradle.kts ํŒŒ์ผ์—์„œ BuildConfig๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ์„ค์ •ํ•œ๋‹ค.

android {
    ....
    buildFeatures {
        ...
        buildConfig = true
    }
}

 

์ฐธ๊ณ 

https://daryeou.tistory.com/324

 

[Gradle] AGP 8.0์—์„œ BuildConfig ์‚ฌ์šฉ ์‹œ ์—๋Ÿฌ

๋ฉ€ํ‹ฐ๋ชจ๋“ˆ์—์„œ BuildConfig๋ฅผ buildType๋ณ„๋กœ ๋‹ค๋ฅด๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ํ•ด๋‹น ๋ชจ๋“ˆ build.gradle ํŒŒ์ผ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ–ˆ์—ˆ๋‹ค. android { namespace 'com.app.xxxx' compileSdk 33 defaultConfig { minSdk 21 targetSdk 33 testInstrumentationRunn

daryeou.tistory.com

 

2. ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

๋‚˜๋Š” libs.version.toml์— dependencies ์ •๋ณด๋“ค์„ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋‹ค.  libs.version.toml์— ํ•ด๋‹น dependecy ์ •๋ณด๋“ค์„ ๊ธฐ์ž…ํ•œ๋‹ค.

[versions]
...
secrets-gradle-plugin = "2.0.1"

[libraries]
...
secrets-gradle-plugin = { group="com.google.android.libraries.mapsplatform.secrets-gradle-plugin", name = "secrets-gradle-plugin", version.ref = "secrets-gradle-plugin" }

[plugins]
...
secrets-gradle-plugin = { id = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin" }

 

2-1. ํ”„๋กœ์ ํŠธ root์˜ build.gradle.kts ํŒŒ์ผ์— ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

plugins {
    ...
}

buildscript {
    dependencies {
        classpath(libs.secrets.gradle.plugin)
    }
}

 

2-2. app์˜ build.gradle.kts ํŒŒ์ผ์— ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

plugins {
    ...
    alias(libs.plugins.secrets.gradle.plugin)
}

 

3. local.properties ํŒŒ์ผ์— API ํ‚ค๋ฅผ ์„ ์–ธํ•œ๋‹ค.

MAP_API_KEY={APIํ‚ค๊ฐ’}

 

4. ๋นŒ๋“œ ํ›„ ์‚ฌ์šฉํ•œ๋‹ค.

4-1. BuildConfig๋กœ ์‚ฌ์šฉ

BuildConfig.MAP_API_KEY

 

4-2. AndroidManifest์—์„œ ์‚ฌ์šฉ

<meta-data android:value="${MAP_API_KEY}" />

 

 

* local.properties ํŒŒ์ผ ์™ธ์— ๋‹ค๋ฅธ ํŒŒ์ผ์— API ํ‚ค๋ฅผ ๊ธฐ์ž…ํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฐธ๊ณ 

https://github.com/google/secrets-gradle-plugin

 

GitHub - google/secrets-gradle-plugin: A Gradle plugin for providing your secrets to your Android project.

A Gradle plugin for providing your secrets to your Android project. - google/secrets-gradle-plugin

github.com

 

https://medium.com/androidbytesensei/safe-your-api-keys-via-secrets-gradle-plugin-1dd9802ef2a6

 

Safe your API Keys via Secrets Gradle plugin

Where should I start about hiding the API keys? It has been coming for a long time that we Android developers try to hide the key by…

medium.com

 


 

๊ธฐ์กด์— local.properties ํŒŒ์ผ์— API ํ‚ค๋ฅผ ๊ธฐ์ž…ํ•˜๊ณ , local.properties๋Š” gitignore ํŒŒ์ผ์— ๊ธฐ์ž…ํ•œ ํ›„์—, build.gradle ํŒŒ์ผ์—์„œ BuildConfig์— local.properties ํŒŒ์ผ์˜ API ํ‚ค ๊ฐ’์„ ๊ฐ€์ ธ๋‹ค๊ฐ€ ์„ ์–ธํ•ด์„œ ์“ฐ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ  ์žˆ์—ˆ๋‹ค.

์ด Secret Gradle Plugin๋Š” BuildConfig์— API ํ‚ค ๊ฐ’์„ ์ž๋™์œผ๋กœ ์„ ์–ธํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์œผ๋กœ ๋ณด์ธ๋‹ค. ์ด๋ฏธ ์•Œ๊ณ  ์žˆ๋˜ ๋ฐฉ๋ฒ•๊ณผ ์ด ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ชจ๋‘ BuildConfig๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ณด๋ฉด, ๊ฒฐ๊ตญ ์›๋ฆฌ๋Š” ๊ฐ™์€ ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.