簡述:
技術預研:
我們找到BuildConfig類以後可以發現該類的路徑爲:包名.BuildConfig.java.如下:
方案設計:
方案一:BuildConfig類中DEBUG屬性
方案二:自定義BuildConfig類
根據預研結論一我們打開BuildConfig類,然後自定義一個屬性,利用該其作爲Logger的開關的屬性。方案實現:
方案一實現:
① 我們直接使用BuildConfig的DEBUG屬性:/**
* @author:lizhenya
* @Time: 2016/11/6
* @email: [email protected]
*/
public class Logger {
private static final String TAG = "Logger";
/**
* The override method of Logger.
*
* The default level of any tag is set to LOGLEVEL 5. This means that any
* level log will be logged. if your set the LOGLEVEL to 0 , no log will be
* print out.
*/
public static int LOGLEVEL = BuildConfig.DEBUG? 5 : 0;
public static boolean VERBOSE = LOGLEVEL > 4;
public static boolean DEBUG = LOGLEVEL > 3;
public static boolean INFO = LOGLEVEL > 2;
public static boolean WARN = LOGLEVEL > 1;
public static boolean ERROR = LOGLEVEL > 0;
public static void setDebugMode(boolean debugMode) {
LOGLEVEL = debugMode ? 5 : 0;
VERBOSE = LOGLEVEL > 4;
DEBUG = LOGLEVEL > 3;
INFO = LOGLEVEL > 2;
WARN = LOGLEVEL > 1;
ERROR = LOGLEVEL > 0;
}
public static void v(String tag, String msg) {
if (DEBUG)
Log.v(tag, msg == null ? "" : msg);
}
public static void v(String tag, String msg, Throwable tr) {
if (DEBUG)
Log.v(tag, msg == null ? "" : msg, tr);
}
public static void v(String msg) {
if (DEBUG)
Log.v(TAG, msg == null ? "" : msg);
}
public static void v(String msg, Throwable tr) {
if (DEBUG)
Log.v(TAG, msg == null ? "" : msg, tr);
}
public static void d(String tag, String msg) {
if (DEBUG)
Log.d(tag, msg == null ? "" : msg);
}
public static void d(String tag, String msg, Throwable tr) {
if (DEBUG)
Log.d(tag, msg == null ? "" : msg, tr);
}
public static void d(String msg) {
if (DEBUG)
Log.d(TAG, msg == null ? "" : msg);
}
public static void d(String msg, Throwable tr) {
if (DEBUG)
Log.d(TAG, msg == null ? "" : msg, tr);
}
public static void e(String tag, String msg) {
if (ERROR)
Log.e(tag, msg == null ? "" : msg);
}
public static void e(String tag, String msg, Throwable tr) {
if (ERROR)
Log.e(tag, msg == null ? "" : msg, tr);
}
public static void e(String msg) {
if (ERROR)
Log.e(TAG, msg == null ? "" : msg);
}
public static void e(String msg, Throwable tr) {
if (ERROR)
Log.e(TAG, msg == null ? "" : msg, tr);
}
}
②使用反射獲取DEBUG屬性值以提高程序的靈活性:首先我們寫一個獲取DEBUG屬性值的工具類
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* @author:lizhenya
* @Time: 2016/11/6
* @email: [email protected]
*/
public class BuildConfigHelper {
public static Boolean isDebug = null;
public static boolean isDebugBuild() {
if (isDebug == null) {
try {
final Class<?> activityThread = Class.forName("android.app.ActivityThread");
final Method currentPackage = activityThread.getMethod("currentPackageName");
final String packageName = (String) currentPackage.invoke(null, (Object[]) null);
final Class<?> buildConfig = Class.forName(packageName + ".BuildConfig");
final Field DEBUG = buildConfig.getField("DEBUG");
DEBUG.setAccessible(true);
isDebug = DEBUG.getBoolean(null);
} catch (final Throwable t) {
final String message = t.getMessage();
if (message != null && message.contains("BuildConfig")) {
isDebug = false;
} else {
isDebug = BuildConfig.DEBUG;
}
}
}
return isDebug;
}
}
其次再在修改Logger類中修改獲取BuildConfig的DEBUG屬性的方式:public static int LOGLEVEL = BuildConfigHelper.isDebug ? 5 : 0;
方案二實現:
我們首先在BuildConfig類中定義一個Logger開關的屬性值:LOG_TOGGLE,完整的BuildConfig類如下:/**
* Automatically generated file. DO NOT MODIFY
*/
package com.lzy.buildconfigdemo;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.lzy.buildconfigdemo";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Fields from build type: debug()我們自定義的一個控制Log開關的屬性值
public static final boolean LOG_TOGGLE = true;
}
我們知道BuildConfig類是Gradle編譯過程中自動生成的,所以我們得把剛纔自定義的屬性配置到build.gradle中,如下:
buildTypes {
release {
buildConfigField "boolean", "LOG_TOGGLE", "false"
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug{
buildConfigField "boolean", "LOG_TOGGLE", "true"
}
}
在上面我們定義了一個: buildConfigField "boolean", "LOG_TOGGLE", "false"
這個語法規則表示:我們定義了一個boolean變量,變量名爲LOG_TOGGLE,並給變量賦值爲false。我們使用如下:
findViewById(R.id.btn_buildConfig).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (BuildConfig.LOG_TOGGLE) {
Toast.makeText(MainActivity.this, "Debug", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Release", Toast.LENGTH_SHORT).show();
}
}
});
OK,方案二流程已經熟悉了,那麼我們現在在封裝Logger類如下:public class Logger {
private static final String TAG = "Logger";
/**
* The override method of Logger.
* <p>
* The default level of any tag is set to LOGLEVEL 5. This means that any
* level log will be logged. if your set the LOGLEVEL to 0 , no log will be
* print out.
*/
public static int LOGLEVEL = BuildConfig.LOG_TOGGLE ? 5 : 0;
public static boolean VERBOSE = LOGLEVEL > 4;
public static boolean DEBUG = LOGLEVEL > 3;
public static boolean INFO = LOGLEVEL > 2;
public static boolean WARN = LOGLEVEL > 1;
public static boolean ERROR = LOGLEVEL > 0;
public static void setDebugMode(boolean debugMode) {
LOGLEVEL = debugMode ? 5 : 0;
VERBOSE = LOGLEVEL > 4;
DEBUG = LOGLEVEL > 3;
INFO = LOGLEVEL > 2;
WARN = LOGLEVEL > 1;
ERROR = LOGLEVEL > 0;
}
public static void v(String tag, String msg) {
if (DEBUG)
Log.v(tag, msg == null ? "" : msg);
}
public static void v(String tag, String msg, Throwable tr) {
if (DEBUG)
Log.v(tag, msg == null ? "" : msg, tr);
}
public static void v(String msg) {
if (DEBUG)
Log.v(TAG, msg == null ? "" : msg);
}
public static void v(String msg, Throwable tr) {
if (DEBUG)
Log.v(TAG, msg == null ? "" : msg, tr);
}
public static void d(String tag, String msg) {
if (DEBUG)
Log.d(tag, msg == null ? "" : msg);
}
public static void d(String tag, String msg, Throwable tr) {
if (DEBUG)
Log.d(tag, msg == null ? "" : msg, tr);
}
public static void d(String msg) {
if (DEBUG)
Log.d(TAG, msg == null ? "" : msg);
}
public static void d(String msg, Throwable tr) {
if (DEBUG)
Log.d(TAG, msg == null ? "" : msg, tr);
}
public static void e(String tag, String msg) {
if (ERROR)
Log.e(tag, msg == null ? "" : msg);
}
public static void e(String tag, String msg, Throwable tr) {
if (ERROR)
Log.e(tag, msg == null ? "" : msg, tr);
}
public static void e(String msg) {
if (ERROR)
Log.e(TAG, msg == null ? "" : msg);
}
public static void e(String msg, Throwable tr) {
if (ERROR)
Log.e(TAG, msg == null ? "" : msg, tr);
}
}