Skip to content

A gradle plugin to show time costs of methods on main-thread for Android.

License

Notifications You must be signed in to change notification settings

xiaobo0113/MethodTimeCost

Repository files navigation

MethodTimeCost

A gradle plugin to show time costs of methods on main-thread.

How to use

Just several lines.

add dependency in the build.gradle of the root project.

dependencies {
    classpath 'com.xiaobo.plugin:method-time-cost:1.1.3'

    ...
}

add dependency in the build.gradle of your application module.

dependencies {
    compile 'com.xiaobo.plugin:method-time-cost_runtime:1.1.1'

    ...
}

Apply the plugin in the build.gradle of your application module.

apply plugin: 'com.xiaobo.method_time_cost'
method_time_cost {
    enable = true           // optional, default is true.
    costBiggerThan = 20     // optional, default is 0. ==> show log of methods cost time >= 0
    tag = "xiaobo"          // optional, default is MethodTimeCost
}

You'd better use the terminal to build apk.

gradlew assembleDebug

How it works

class Test {
    public int add (int a, int b) {
        int result = a + b;
        return result;
    }
}

will change into

class Test {
    static LinkedList<Long> __time_list = new LinkedList<Long>();
    public int add (int a, int b) {
        __time_list.push(System.currentTimeMillis());
        int result = a + b;
        Log.d(TAG, "" + System.currentTimeMillis() - __time_list.pop());
        return result;
    }
}

The code is inserted by Java Assist.

Suggestion

Just use the plugin under debug mode. For it will insert code into your source class files, which will increases the size of your apk.

Others

  1. A few methods can not be monitored since the exception from Java Assist.
  2. You can use AspectJ instead. But you will write more code and it will generate too many classes.

About

A gradle plugin to show time costs of methods on main-thread for Android.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published