- 
                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:PLUGIN_VERSION_NAME"
    }
}PLUGIN_VERSION_NAME MethodCanary github release
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 GodEyeHelperclass
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 AndroidGodEye-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
AndroidGodEye-MethodCanary.js should be placed in the root directory of your project. An example of the file content is as follows:
/**
    classInfo
        {int access
         String name
         String superName
         String[] interfaces}
     methodInfo
         {int access
         String name
         String desc}
**/
function isInclude(classInfo,methodInfo){
    if(!classInfo.name.startsWith('cn/hikyson/methodcanary')){
        return false;
    }
    if(classInfo.name.startsWith('cn/hikyson/methodcanary/samplelib/R$')
            || classInfo.name === 'cn/hikyson/methodcanary/samplelib/BuildConfig'){
            return false
    }
    return true
}The meaning of the above example is: All methods start with the class name of cn/hikyson/methodcanary need to be injected code, but exclude cn/hikyson/methodcanary/samplelib/R$ and cn/hikyson/methodcanary/samplelib/BuildConfig.
- The names and parameters of the method in the file cannot be modified: function isInclude(classInfo,methodInfo)
- The return values of the method must be bool type, isIncludemethod returns true by default
- The parameter classInfohas the following fields:int access,String name,String superName,String[] interfaces
- The parameter methodInfohas the following fields:int access,String name,String desc
- Write AndroidGodEye-MethodCanary.jsfile in JavaScript language