Skip to content

0x03a MethodCanary_en

hui.zhao edited this page Nov 24, 2019 · 6 revisions

MethodCanary

Extra dependencies

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'

Install

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

Production and consumption of data

Click the start button to start recording on debug monitor dashboard, and click the end button to end recording and output the time-consuming of the methods (as follows), 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

DebugMonitor Dashboard

On the dashboard, click start, operate app, then click end to view the time consumption of methods

android_god_eye_method_canary_dashboard

Advantages over Android studio CPU profiler

  • 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

Instructions of MethodCanary.js

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

Attentions

  1. The names and parameters of these methods in the file cannot be modified: function isExclude(classInfo,methodInfo) and function isInclude(classInfo,methodInfo)
  2. The return values of these methods must be bool type, the isExclude method returns false by default, and the isInclude method returns true by default
  3. The parameter classInfo has the following fields: int access,String name,String superName,String[] interfaces
  4. The parameter methodInfo has the following fields: int access,String name,String desc
  5. Write MethodCanary.js file in JavaScript language
Clone this wiki locally