Android調試自測工具01 (Hugo、Timber、Scalpel)
Hugo
做項目的時候有時候需要打印方法的傳參和返回值,甚至方法的執行時間,有沒有一種簡單方便通用的方式去做這個呢,Hugo就可以。
使用方法很簡單,Hugo是基于注解被調用的,引入相關依賴后,在方法上加上 @DebugLog 即可。也可以加在內部類上。
我沒研究在Eclipse下怎么引入這個東西,用Eclipse的同志趁早轉投AndroidStudio吧。AndroidStudio下引入Hugo很方便,添加兩行代碼即可。
先看看打印的效果圖:

配置的流程:
Project級別的build.gradle dependencies 內加入
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
} Module級別的build.gradle 頂部加入
apply plugin: 'com.jakewharton.hugo'
然后代碼中加入注解即可
@DebugLog
private int fibonacci(int number) {
if (number <= 0) {
throw new IllegalArgumentException("Number must be greater than zero.");
}
if (number == 1 || number == 2) {
return 1;
}
// NOTE: Don't ever do this. Use the iterative approach!
return fibonacci(number - 1) + fibonacci(number - 2);
}
@DebugLog
class Charmer {
private final String name;
Charmer(String name) {
this.name = name;
}
public String askHowAreYou() {
return "How are you " + name + "?";
}
} Timber
Timber其實就是對Android的Log類進行封裝后的一個Log工具,平時我自己也有封裝過,不過大神的封裝非常優雅。
Timber只有一個類文件,可以單獨把它復制出來放項目里,也可以通過Gradle引用:
compile 'com.jakewharton.timber:timber:2.7.1'
Timber使用的時候推薦在Application類中初始化,比如:
public class DemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
} else {
Timber.plant(new CrashReportingTree());
}
}
} Timber.plant(Tree tree)用來給Timber設置用于打印的實現類,Tree是個接口,DebugTree是Timber中已經實現了tree的類,可直接拿來用。Timber中還有個HollowTree的類用于擴展,比如上面的CrashReportingTree,我們可以把崩潰打印進行一些處理。使用的時候調用靜態方法即可:
Timber.tag("LifeCycles");//設置只能用一次的Tag
Timber.d("Activity Created");
//DebugTree 會幫你進行格式化輸出
Timber.i("A button with ID %s was clicked to say '%s'.", id, messag); 其他的查看源碼吧。
Scalpel
這個可以查看界面的圖層,3D的效果,不像開發者選項中 開啟顯示布局邊界 是平面的線框。用AndroidStudio 引用只要在 build.gradle添加
compile 'com.jakewharton.scalpel:scalpel:1.1.2'
使用的時候你的layout根節點必須是 ScalpelFrameLayout , ScalpelFrameLayout有以下幾個常用方法:
開啟3D效果 : setLayerInteractionEnabled(boolean).
顯隱DrawViews:setDrawViews(boolean).
顯隱 view ID: setDrawIds(boolean).
修改邊框的顏色和陰影 setChromeColor(int) and setChromeShadowColor(int).
手勢操作的方法:
-
單指用來旋轉整個模型
-
雙指垂直收縮來調整模型大小
-
雙指水平收縮來調整每個圖層間的間距
效果圖:


最后是這三個工具的DEMO:
來自: http://tech.luffyjet.com/2015/04/10/android-debug-tools-hugo-scalpel-timber/