Android性能优化-几款检测工具的集成与实战使用

收到中软国际外包到UC的offer,业务是UC国内浏览器用户增长,询问了一些师兄UC的情况并这是外包编制,综合考量下,接受这个offer的概率不会很大,又回归到找人内推的状态啦~~
今天把常用的几款检测性能的工具都整理到一个Library中,并且按照debug和release区别Application和AndroidManifest.xml。

列表

名称 作用 官网地址 集成难度
LeakCanary 内存泄漏检测 https://github.com/square/leakcanary 简单
BlockCanary 页面卡顿检测 https://github.com/markzhai/AndroidPerformanceMonitor 简单
UETool UE调试工具 https://github.com/eleme/UETool 简单但有点小坑
GT APP的随身调试平台 https://github.com/Tencent/GT 部分机型集成会崩

原理

LeakCanary

1.Application注册对Activity生命周期的回调监听;
2.WeakReference对Activity的包装;
3.强制GC操作,System.gc()源码;
4.检测Activity是否被回收;
具体分析参考:LeakCanary源码分析-苍王

BlockCanary

Loop的loop()方法:

public static void loop() {
    ...
    for (;;) {
        ...
        // This must be in a local variable, in case a UI event sets the logger
        Printer logging = me.mLogging;
        if (logging != null) {
            logging.println(">>>>> Dispatching to " + msg.target + " " +
                    msg.callback + ": " + msg.what);
        }
        msg.target.dispatchMessage(msg);
        if (logging != null) {
            logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
        }
        ...
    }
}

Message分发前后都有一句日志打印,自定义Printer,然后记录两句日志的打印时间,差值和设置的阙值对比,得出页面是否卡顿。
具体分析参考:BlockCanary — 轻松找出Android App界面卡顿元凶

GT随身调

GT检测的功能比较多,包含(CPU、内存、流量、电量、帧率/流畅度等等)、开发日志的查看、Crash日志查看、网络数据包的抓取、APP内部参数的调试、真机代码耗时统计等。
具体分析参考:
布局检测原理及规则
流畅性检测原理及规则
页面启动时长检测原理及规则

实战 (仅在Debug包使用)

发现问题,问题就解决一半:

虽然每一个检测工具集成都不是非常难,并且在对开发者的帮助还是显而易见的,但是面临一个问题就是都需要在Application中初始化,但是实际Release包中却是要剔除这些调试包(GT在内测的时候还仅仅提供jar+so的引用方式)。

解决方式:

1.建立一个Library,所有的debug调试工具包都集中依赖;

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // ele ue tool
    debugCompile ('me.ele:uetool:1.0.15') {
        exclude module: 'support-v4'
        exclude module: 'appcompat-v7'
        exclude module: 'recyclerview-v7'
    }
    releaseCompile 'me.ele:uetool-no-op:1.0.15'
    // if you want to show more attrs about Fresco's DraweeView
    // debugCompile 'me.ele:uetool-fresco:1.0.15'

    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.4'
    //releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'

    // most often used way, enable notification to notify block event
    //compile 'com.github.markzhai:blockcanary-android:1.5.0'
    // this way you only enable BlockCanary in debug package
     debugCompile 'com.github.markzhai:blockcanary-android:1.5.0'
    // releaseCompile 'com.github.markzhai:blockcanary-no-op:1.5.0'
}

2.主模块App依赖Library,建立debug版的DebugApplication以及AndroidManifest.xml

3.主模块App的build.gradle中根据debug与否选择DebugApplication (初始化各类检测工具) 还是ReleaseApplication。

android {
    ...
    sourceSets {
        main {
            if (debug) {
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
            } else {
                manifest.srcFile 'src/main/AndroidManifest.xml'
                java {
                    exclude 'muugi/**'//包名
                    exclude 'src/main/debug/AndroidManifest.xml'
                }
            }
            java.srcDirs = ['src/main/java', 'src/main/debug']
        }
    }
    ...
}

4.DebugApplication继承Release版本的Application

public class DebugApplication extends RiotGameApplication {

    private static final String TAG = "muugi.DebugApplication";

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate: ");
        //初始化Release版Application
        super.onCreate();
        //初始化检测工具
        initBlockCanary();
        initLeakCanary();
        initEleUeTool();
        initGT();
    }

    /**
     * 初始化GT性能检测工具.
     */
    private void initGT() {
        GTRController.init(this);
    }

    /**
     * 初始化页面卡顿检测工具.
     */
    private void initBlockCanary() {
        BlockCanary.install(this, new AppBlockCanaryContext()).start();
    }

    /**
     * 初始化内存泄漏检测工具.
     */
    private void initLeakCanary() {
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);
    }

    /**
     * 初始化饿了么UE调试工具.
     */
    private void initEleUeTool() {
//        UETool.putFilterClass(FilterOutView.class);
//        UETool.putAttrsProviderClass(CustomAttribution.class);

        registerActivityLifecycleCallbacks(new EleActivityLifecycleCallbacks());
    }

}

效果图:

总结:

1.项目例子是高仿掌上英雄联盟APP,目前完成度还是比较低的,一边复习知识点一边完成吧,问题不大。
地址:https://github.com/scauzhangpeng/RiotGame

2.GT给出的性能检测结果还是比较有参考意义的,可以根据检测的结果进行一系列的优化。

3.UETool适合自测阶段UI设计师,产品对界面的存在货是否对板的检测。

猜你喜欢

转载自blog.csdn.net/scau_zhangpeng/article/details/81007051