안드로이드 프로젝트에서 설정 값들은 대부분 values/strings.xml에 등록해 놓는데 strings에 입력하지 않고 별도의 파일에 입력을 하지만 strings에 있는것 처럼 사용할 수 있는 방법입니다.
Properties() 함수를 이용하면 되는데 사용방법은
secure.properties 파일을 프로젝트 내 임의의 위치에 생성하고 내용에
MAPS_API_KEY=AIxxxxxxXXXXXXxxxxxxxXXXXXXXxxxxxxXxN_K8
이런 식으로 설정 값을 입력 합니다.
그리고 build.gradle(Module) 파일에 설정한 값을 읽어 올 수 있도록 Properties 함수를 추가 합니다.
defaultConfig {
applicationId "copycoding.tistory.mylocation"
minSdkVersion 16
targetSdkVersion
29
versionCode 1
versionName "1.0"
testInstrumentationRunner
"androidx.test.runner.AndroidJUnitRunner"
// Read the API
key from ./secure.properties into R.string.maps_api_key
def secureProps = new Properties()
if (file("../secure.properties").exists()) {
file("../secure.properties")?.withInputStream
{ secureProps.load(it) }
}
resValue "string", "maps_api_key",
(secureProps.getProperty("MAPS_API_KEY") ?: "")
}
이렇게 하면 secure.properties 파일에 설정한 값을 가져옵니다. 파일 명과 파일 위치는 어디에 생성 했는가에 따라 수정을 하면 됩니다.
이제 AndroidManifest.xml 에서 설정 값을 사용하는 방법 입니다.
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/maps_api_key" />
그냥 strings.xml에 입력해 놓은 것 처럼 동일한 방법으로 사용이 가능 합니다.
- copy coding -
'Android' 카테고리의 다른 글
[Android] 안드로이드 옵션 메뉴 (Option Menu) 생성 (6) | 2020.08.27 |
---|---|
[Google Map] Android google places API not authorized (0) | 2020.07.09 |
Android Google Maps SDK 사용 예제 (0) | 2019.12.03 |
Could not find any version that matches (0) | 2019.10.27 |
android studio 3.x 2.x 예전 구버전 다운로드 (0) | 2019.10.13 |