通过 build.gradle 编译生成BuildConfig.java类
buildTypes {
debug {
buildConfigField("boolean", "LOG_ENABLE", "true")
}
release {
buildConfigField("boolean", "LOG_ENABLE", "false")
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig android.signingConfigs.release
}
}
其中, buildConfigField这行就是自定义的BuildConfig字段
项目编译后的BuildConfig类如下:
/**
* Automatically generated file. DO NOT MODIFY
*/
package com.hola.weather;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.hola.weather";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 10010000;
public static final String VERSION_NAME = "1.0.0";
// Fields from build type: debug
public static final boolean LOG_ENABLE = true;
}
可见, 生成了自定义的"LOG_ENABLE"字段.
在需要判断是否是release版的位置这样使用
public class JLog {
private static final boolean DEBUG = BuildConfig.LOG_ENABLE;
... ...
}
refer to:
http://blog.csdn.net/buaaroid/article/details/49496779