Skip to content

0x03a MethodCanary_en

hui.zhao edited this page Feb 29, 2020 · 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: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'

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

You can produce data in two ways

  1. 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)
  2. 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

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

android_god_eye_method_canary

Advantages over Android studio CPU profiler

  • 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

Instructions of AndroidGodEye-MethodCanary.js

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.

Attentions

  1. The names and parameters of the method in the file cannot be modified: function isInclude(classInfo,methodInfo)
  2. The return values of the method must be bool type, 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 AndroidGodEye-MethodCanary.js file in JavaScript language
Clone this wiki locally