Android Studio通過配置build.gradle參數(shù)設(shè)置全局的Log開關(guān)
在app中的build.gradle中梅惯,在編譯后會生成一個BuildConfig的類,這個類包含了一些當(dāng)前編譯類型的基本參數(shù)。如:
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參數(shù)其實是通過isDebuggable參數(shù)來控制的
一般debug類型apk是默認(rèn)BuildConfig.DEBUG為true,release類型BuildConfig.DEBUG為false。
buildTypes {
release {
...
isDebuggable = false //默認(rèn)false原茅,可不寫
}
debug {
...
isDebuggable = true //默認(rèn)true,可不寫
}
}
當(dāng)然我們還可以在build.gradle中自定義一個變量控制,如:
defaultConfig {
...
buildConfigField("boolean", "IS_SHOW_LOG", "false")//TODO Log日志開關(guān)
}
會在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默認(rèn)禁用了生成BuildConfig
項目中使用productFlavors配置了buildConfigField 提示功能禁用
解決辦法:開啟buildConfig
buildFeatures {
buildConfig = true
}