Android Studio에서 Gradle로 앱을 빌드하면 BuildConfig 클래스가 생성되며, 이 클래스를 통해 Package name, Version, Debug 상태 등의 정보를 얻을 수 있습니다.
BuildConfig
App이 빌드될 때 BuildConfig.java
파일이 생성됩니다.
파일을 보면 다음과 같은 상수들이 있습니다.
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.example.example";
public static final String BUILD_TYPE = "debug";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
}
이런 상수들중 대부분은 App의 build.gradle
에 있는 defaultConfig에서 가져옵니다.
상수 중에 BUILD_TYPE
은 release 또는 debug 빌드에 따라서 다르게 설정됩니다. DEBUG
또한 build type에 따라 true, false가 리턴됩니다.
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.example"
minSdkVersion 26
targetSdkVersion 30
versionCode 2
versionName "1.0"
}
....
}
BuildConfig에 상수 추가
BuildConfig에 원하는 상수를 추가하려면 다음과 같이 buildConfigField()
를 사용할 수 있습니다.
android {
...
buildTypes {
release {
buildConfigField("String", "DOWNLOAD_URL", '"https://test.com/release/"')
}
debug {
buildConfigField("String", "DOWNLOAD_URL", '"https://test.com/debug/"')
}
}
...
}
위와 같이 추가하고 빌드를 하면, 코드에서 참조가 가능합니다.
Log.d("Test", "DOWNLOAD_URL: ${BuildConfig.DOWNLOAD_URL}")
DEBUG로 빌드한 앱을 실행해보면 다음과 같이 출력됩니다.
12-23 22:46:14.884 7153 7153 D Test : DOWNLOAD_URL: https://test.com/debug/
다른 자료형의 상수 추가
String외에 boolean이나 int를 추가하려면 다음과 같이 사용할 수 있습니다.
buildConfigField("boolean", "USE_CACHE", "true")
buildConfigField("int", "CACHE_SIZE", "50")
Recommended Posts:
- Android 11 - 시스템 기본 카메라 정책
- Android 11 - Privacy 변경사항 요약
- Android11 - Storage(저장소) 정책 변경사항 정리
- Android11 - Package visibility 변경사항 소개
- Android - File-Based Encryption과 Direct Boot 이해하기
- 안드로이드 Q - Thermal API로 발열 상태 모니터링
- 안드로이드 Q - Background Activity 실행 제한 정책
- Android App Bundle로 Apk 크기를 더 작게 만들기
- 안드로이드 Q - Background Location 권한 제한 정책
- 안드로이드 Q - 새로운 저장소 정책, Scoped Storage 알아보기
- 안드로이드 Q - Settings Pannel에 대해서 알아보기
- 안드로이드 Q - Mainline, APEX에 대해서 알아보기
- 안드로이드 - JobScheduler로 백그라운드 작업 실행하는 방법