[Kotlin][API Key보관]local.properties 이용(오류 해결)

Boknami·2023년 8월 31일
0

코틀린

목록 보기
11/19

https://velog.io/@shin75492/Kotlin-API-Key-%EC%95%88%EC%A0%84%ED%95%98%EA%B2%8C-%EB%B3%B4%EA%B4%80%ED%95%98%EA%B8%B0

이미 이 전에 다 만든 내용인데 이상하게 자꾸 API Key를 불러오는데 오류가 발생해서 리팩토링했다.

Build file 'C:\Users\a\AndroidStudioProjects\bc\app\build.gradle' line: 18

A problem occurred evaluating project ':app'.
> Could not get unknown property 'NaverMapApiKey' for DefaultConfig$AgpDecorated_Decorated{name=main, dimension=null, minSdkVersion=DefaultApiVersion{mApiLevel=24, mCodename='null'}, targetSdkVersion=DefaultApiVersion{mApiLevel=33, mCodename='null'}, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode=1, versionName=1.0, applicationId=com.beacon, testApplicationId=null, testInstrumentationRunner=null, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=[], buildConfigFields={}, resValues={}, proguardFiles=[], consumerProguardFiles=[], manifestPlaceholders={}, wearAppUnbundled=null} of type com.android.build.gradle.internal.dsl.DefaultConfig$AgpDecorated.
buildConfigField "String", "NAVER_MAP_API_KEY", "\"${localProperties['NaverMapApiKey']}\""

buildConfigField를 사용하는데 제대로 값을 못 불러오는 것 같았다. 그러니까 계속 manifest에서 오류가 발생하고 잘 안됐다..

참고한 내용들이 이 전 버전의 안드로이드 스튜디오 버전이라 아마 오류가 많이 발생하는 것 같았다.

📌 해결 방법

  1. local.properties에 API_KEY보관
NaverMapApiKey=API_KEY(이 전에 했던 것처럼 하지 않고 따옴표를 없앴다!)
  1. local.properties에 저장된 값을 build.gradle에 불러온다!
def localProperties = new Properties()
localProperties.load(new FileInputStream(rootProject.file("local.properties")))
  1. 다른 곳에서 사용할 수 있도록!
android {
    namespace 'com.beacon'
    compileSdk 33

    defaultConfig {
        applicationId "com.beacon"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"
        
        
        //<----------이 부분이 로컬프로퍼티에 저장한 값을 불러 처리---------->
        buildConfigField "String", "NAVER_MAP_API_KEY", "\"${localProperties['NaverMapApiKey']}\""
        resValue "string", "NAVER_MAP_API_KEY_VALUE", "\"${localProperties['NaverMapApiKey']}\""

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
  1. mainfest에서 사용!
<meta-data
            android:name="com.naver.maps.map.CLIENT_ID"
            android:value="@string/NAVER_MAP_API_KEY_VALUE" />

=> 이상 없이 잘 해결! 230831기준으로 문제없음!

0개의 댓글