๐Ÿ”’ API KEY ์ˆจ๊ธฐ๊ธฐ

kkominยท2023๋…„ 10์›” 20์ผ
0

Android Studio

๋ชฉ๋ก ๋ณด๊ธฐ
40/44
post-thumbnail

์ด๊ฑด ๋ชฐ๋ผ์„œ ์•ˆ๋˜๋Š” ! ํ•„์ˆ˜์ ์œผ๋กœ ์•Œ์•„์•ผ ํ•˜๋Š” ์ƒ์‹ !


์™œ ์ˆจ๊ฒจ์•ผ ํ•˜๋Š”๊ฑฐ์•ผ? ๐Ÿค”

api๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ฑฐ๋‚˜ ํ•  ๋•Œ ์šฐ๋ฆฌ๋Š” API KEY๋Š” ๊ฐœ๋ฐœ์ž๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ๋‹ค๋ฅธ์ด๋‚˜ ์‚ฌ์šฉ์ž๊ฐ€ ์•Œ์•„์„œ๋Š” ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊นƒํ—™์— commit์— api key๊ฐ€ ๋‚จ์•„์žˆ์œผ๋ฉด ์ ˆ๋Œ€๋กœ ์•ˆ๋œ๋‹ค.

API KEY๋Š” ์šฐ๋ฆฌ์˜ ๊ฐœ์ธ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” ์ค‘์š”ํ•œ ๊ฒƒ

๋งŒ์•ฝ ํ”„๋กœ์ ํŠธ ๊ฐœ๋ฐœ์— ์ž„ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค ์ด์™ธ์˜ ํƒ€์ธ์ด ์•Œ๊ฒŒ ๋œ๋‹ค๋ฉด, api๋ฅผ ๋ˆ„๊ตฌ๋‚˜ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ด์ง€๊ณ  ์ด๋กœ ์ธํ•ด ์ตœ์•…์˜ ๊ฒฝ์šฐ์—๋Š” ์•ฑ์˜ ์„œ๋น„์Šค๋ฅผ ์ข…๋ฃŒํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.


Commit ๊ธฐ๋ก์— ์•ˆ๋‚จ๊ธฐ๋Š” ๋ฐฉ๋ฒ•

local-properties

local properties์— ๋จผ์ € api ํ‚ค๋ฅผ ๋“ฑ๋กํ•ด์ค€๋‹ค.

sdk.dir=C\:\\Users\\ASUS\\AppData\\Local\\Android\\Sdk
weather_api_key = "๋ฐ›์•„์˜จ api ํ‚ค ์ž…๋ ฅ"

gitignore

gitignore์— local properties๋ฅผ ๋„ฃ์–ด์„œ ๊นƒํ—™์—์„œ ์ด๋ฅผ ๋ฌด์‹œํ•˜๋„๋ก ์„ค์ •ํ•ด์ค€๋‹ค.


build.gradle(app)

app gradle์—์„œ ์ƒˆ๋กœ ๋“ฑ๋กํ•œ ํ‚ค๊ฐ’์„ ๋ถˆ๋Ÿฌ์™€์ฃผ๊ณ , ํ”„๋กœ์ ํŠธ์—์„œ ๋“ฑ๋กํ•œ api๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํƒ€์ž… - ํ‚ค - ์‚ฌ์šฉ ํ˜•์‹์œผ๋กœ ์ €์žฅํ•ด์ฃผ๊ณ  buildConfigField์— ์ €์žฅ๋œ api๋ฅผ ๋ถˆ๋Ÿฌ์™€์ฃผ๋ฉด ๋œ๋‹ค.

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'com.google.gms.google-services'
    id 'kotlin-kapt'
    id 'kotlin-parcelize'
}
// ์„ ์–ธ ๋ฐ ํ‚ค ๊ฐ’ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
	...		// ์ดํ•˜
defaultConfig {
        applicationId "com.example.sansaninfo"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"
        // ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ( ํƒ€์ž… - ํ‚ค - ์‚ฌ์šฉ ) ์ €์žฅ
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        buildConfigField("String", "WEATHER_API_KEY", properties['weather_api_key'])
    }

BuildConfig

BuildConfig์— ํƒ€์ž… - ํ‚ค - ์‚ฌ์šฉ ๋Œ€๋กœ ์ €์žฅ์ด ์ž˜ ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธ๋งŒ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด api๊ฐ€ ํ”„๋กœ์ ํŠธ์— ์ œ๋Œ€๋กœ ๋“ฑ๋ก์ด ๋œ ๊ฒƒ์ด๋‹ค.

profile
์†Œ์†Œํ•œ ์ฝ”๋”ฉ ์ผ๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€