Android Studio通过配置build.gradle参数设置全局的Log开关
在app中的build.gradle中,在编译后会生成一个BuildConfig的类,这个类包含了一些当前编译类型的基本参数。如:
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.ateqi.xhttp";
public static final String BUILD_TYPE = "debug";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
}
BuildConfig中的DEBUG参数其实是通过isDebuggable参数来控制的
一般debug类型apk是默认BuildConfig.DEBUG为true,release类型BuildConfig.DEBUG为false。
buildTypes {
release {
...
isDebuggable = false //默认false,可不写
}
debug {
...
isDebuggable = true //默认true,可不写
}
}
当然我们还可以在build.gradle中自定义一个变量控制,如:
defaultConfig {
...
buildConfigField("boolean", "IS_SHOW_LOG", "false")//TODO Log日志开关
}
会在BuildConfig这个类中生成一个变量,变量名为IS_SHOW_LOG,值为false
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.ateqi.xhttp";
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 boolean IS_SHOW_LOG = false;
}
gradle8.x默认禁用了生成BuildConfig
项目中使用productFlavors配置了buildConfigField 提示功能禁用
解决办法:开启buildConfig
buildFeatures {
buildConfig = true
}