Android Hugo 插件

Hugo的功能

除butterknife之外,个人同样喜欢Hugo,因为其简便的特性。
Hugo可以帮助code进行简单的性能优化。它会在log中,自动打印某一方法的输入参数值,返回结果值,以及方法耗时。

如何使用Hugo

使用Hugo只需要三个步骤
(1)在Android Project的build.gradle文件中,添加Hugo的dependence


buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1'//关键所在
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

(2)在相应Module的build.gradle文件中添加Hugo Plugin的应用

apply plugin: 'com.android.application'
apply plugin:'com.jakewharton.hugo'//关键所在

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.breakloop.hugodemo"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    testCompile 'junit:junit:4.12'
}

(3)在相应的方法前,添加@DebugLog

package com.breakloop.hugodemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import hugo.weaving.DebugLog;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int res=add(3,4);
    }

    @DebugLog //关键所在
    private int add(int a, int b){
        return a+b;
    }
}

查看输出

10-14 09:13:41.812 13911-13911/com.breakloop.hugodemo V/MainActivity: ⇢ add(a=3, b=4)
10-14 09:13:41.812 13911-13911/com.breakloop.hugodemo V/MainActivity: ⇠ add [0ms] = 7

使用Hugo后,我们可以摆脱不必要的log代码。

此处需要重点说明

调用Hugo后,Hugo自动生效。由于仅在Debug中生效,因此,可以不用关闭。网上各个文章中所说的控制开关,例如

hugo {
  enabled false
}

或者,在Code中调用

Hugo.setEnabled(true|false)

至少在Android Studio中皆无效。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值