반응형
BuildConfig
설명
- gradle 설정값을 java 코드로 접근할 수 있게 하는 클래스
- build.gradle 변경 후 sync시에도 BuildConfig에 반영되지 않을 경우 Rebuild Project 한 번 수행 후 재확인
기본 예제
build.gradleandroid {
defaultConfig {
...
buildConfigField "String", "TEST_MESSAGE", '"Hello World"'
}
}
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.example.android_example";
public static final String BUILD_TYPE = "debug";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Field from default config.
public static final String TEST_MESSAGE = "Hello World";
}
환경별 설정값 분리 예제
~/app/config-common.propertiesAPP_NAME="android-example"
~/app/config-debug.properties
MESSAGE="Hello Debug"
URL="http://dev.example.com"
~/app/config-release.properties
MESSAGE="Hello Release"
URL="http://real.example.com"
build.gradle
android {
defaultConfig {
...
loadProperties("config-common.properties").each { buildConfigField("String", it.key, it.value) }
}
buildTypes {
debug {
...
loadProperties("config-debug.properties").each { buildConfigField("String", it.key, it.value) }
}
release {
...
loadProperties("config-release.properties").each { buildConfigField("String", it.key, it.value) }
}
}
}
def loadProperties(fileName) {
def properties = new Properties()
properties.load(new FileInputStream(file(fileName)))
return properties
}
BuildConfig
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.example.android_example";
public static final String BUILD_TYPE = "debug";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Field from default config.
public static final String APP_NAME = "android-example";
// Field from build type: debug
public static final String MESSAGE = "Hello Debug";
// Field from build type: debug
public static final String URL = "http://dev.example.com";
}
참고
반응형
'Development > Android' 카테고리의 다른 글
[Android] ViewTreeObserver (0) | 2021.02.10 |
---|---|
[Android] LifecycleObserver (0) | 2021.02.10 |
[Android] meta-data (0) | 2021.02.09 |
[Android] Kotpref (0) | 2021.02.09 |
[Android] SharedPreferences (0) | 2021.02.09 |