-
Notifications
You must be signed in to change notification settings - Fork 347
0x03a MethodCanary_en
Methodcanary needs to add some code in your project, so it needs to add gradle plugin first.
In build.gradle of the project root directory
buildscript {
dependencies {
classpath "cn.hikyson.methodcanary:plugin:0.12.1"
}
}
Then in the build.gradle of the main module (apply plugin: 'com.android.application')
apply plugin: 'cn.hikyson.methodcanary.plugin'
Use the following configuration to install
GodEye.instance().install(GodEyeConfig.defaultConfigBuilder().withMethodCanaryConfig(new GodEyeConfig.MethodCanaryConfig(int maxMethodCountSingleThreadByCost, long lowCostMethodThresholdMillis)).build());
or
<methodCanary maxMethodCountSingleThreadByCost="300" lowCostMethodThresholdMillis="10"/>
- maxMethodCountSingleThreadByCost: the maximum number of methods displayed by a single thread, sorted by method time consumption, 300 by default
- lowCostMethodThresholdMillis: the lowest threshold value of method time consumption. Methods consumption below the threshold value will not displayed. The default value is 10ms
You can produce data in two ways
- In the debug monitor dashboard, click the start button to start recording, and click the end button to end recording (in fact, it will call API in the second way)
- Call API in
GodEyeHelper
class
try {
// method canary start recording
GodEyeHelper.startMethodCanaryRecording("tag");
} catch (UninstallException e) {
e.printStackTrace();
}
try {
// method canary stop recording
GodEyeHelper.stopMethodCanaryRecording("tag");
} catch (UninstallException e) {
e.printStackTrace();
}
and use the following methods to observe the output:
try {
GodEye.instance().observeModule(GodEye.ModuleName.METHOD_CANARY, new Consumer<MethodsRecordInfo>() {
@Override
public void accept(MethodsRecordInfo methodsRecordInfo) throws Exception {
}
});
} catch (UninstallException e) {
e.printStackTrace();
}
methodsRecordInfo
records every method's cost time on all threads
On the dashboard, click start, operate app, then click end to view the time consumption of methods
- MethodCanary only records the time-consuming of the methods you care about (by configuring MethodCanary.js), while Android studio CPU profiler records all the methods include Android framework's
- MethodCanary is very fast, there is almost no delay start and end, while Android studio CPU profiler usually needs a long analysis time
- MethodCanary supports the time-consuming method tree of presentation methods, including time-consuming and proportion
MethodCanary.js
is placed in the root directory of your project, An example of the file content is as follows:
function isExclude(classInfo,methodInfo){
if(
classInfo.name.startsWith('cn/hikyson/methodcanary/sample/R$')
|| classInfo.name === 'cn/hikyson/methodcanary/sample/BuildConfig'){
return true
}
return false
}
function isInclude(classInfo,methodInfo){
return classInfo.name.startsWith('cn/hikyson/methodcanary')
}
The meaning of the above example is: all methods start with the class name of cn/hikyson/methodcanary
need to be inserted, but the method names include cn/hikyson/methodcanary/sample/R$
or cn/hikyson/methodcanary/sample/BuildConfig
need to be excluded
- The names and parameters of these methods in the file cannot be modified:
function isExclude(classInfo,methodInfo)
andfunction isInclude(classInfo,methodInfo)
- The return values of these methods must be bool type, the
isExclude
method returns false by default, and theisInclude
method returns true by default - The parameter
classInfo
has the following fields:int access
,String name
,String superName
,String[] interfaces
- The parameter
methodInfo
has the following fields:int access
,String name
,String desc
- Write
MethodCanary.js
file in JavaScript language