Android调试自测工具01 (Hugo、Timber、Scalpel)

jopen 8年前

三个工具都出自JakeWharton大神。hugo和timber用于打印Log, scalpel用于在3D下查看界面的图层。

Hugo

做项目的时候有时候需要打印方法的传参和返回值,甚至方法的执行时间,有没有一种简单方便通用的方式去做这个呢,Hugo就可以。
使用方法很简单,Hugo是基于注解被调用的,引入相关依赖后,在方法上加上 @DebugLog 即可。也可以加在内部类上。

我没研究在Eclipse下怎么引入这个东西,用Eclipse的同志趁早转投AndroidStudio吧。AndroidStudio下引入Hugo很方便,添加两行代码即可。

先看看打印的效果图:

hugo_logs

配置的流程:

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).

手势操作的方法:

  1. 单指用来旋转整个模型

  2. 双指垂直收缩来调整模型大小

  3. 双指水平收缩来调整每个图层间的间距

效果图:

scalpel_preview1

scalpel_preview1

最后是这三个工具的DEMO:

APK

Source