MethodTimeCost
August 12, 2020 ยท View on GitHub
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
- A few methods can not be monitored since the exception from Java Assist.
- You can use
AspectJinstead. But you will write more code and it will generatetoo manyclasses.