(B) App start-up time optimization of code implementation

Confucius said: Reviewing the Old, can serve as a teacher. "Analects of Confucius" - Confucius


The second column as performance optimization, before reading this article can be read (a) Android solutions as well as the origin of black and white screen , then read this article better effect.

App a perfect start, will no doubt give the user the first time the application of a good experience.

When building App, we often need to refer to some third-party sdk, but with the increase in project business, citing third-party libraries more, some third party claims in Application of our onCreate () method to initialize it. This means: in Applicationthe onCreate()time of the execution of the method 越长, the first Activity layout rendering time will be a corresponding elongated.

Similarly, if we are in Activity 的 onCreate(),onStart(),onResume() 方法中执行的任务时间过长the same layout rendering time can lead to stretch. Such a direct result of the problem, the user will feel no delay in page load out, a sense of poor user experience, to the time the product or operators might put the sword to kill over three meters, ha ha, a joke.

As a qualified developer, we know that App called by the system, and then to the first page rendering to the screen. We usually only need to focus on Apolication 中的 onCreate() 方法, 第一个 Activity 中的onCreate() ,onStart() ,onResume() 方法. Note that: If at the first start App Activity, the Activity will not only have their own logic, still onCreate (), onStart () or onResume () method in direct and jump to other pages Activity, then 跳转后的 Activity 的这三个方法也需要进行优化.

Well, Rory it repetitious to say a bunch, then we are most concerned about is how to optimize, do not worry Oh, come specifically Here are some solutions to ensure good faith is full of dry goods.


1. App startup time detecting

Enter the following command in a terminal Android Studio:

adb shell am start -W [项目包名]/[要启动的Activity的名字]
例如:
adb shell am start -W com.imooc.imooc_voice/com.imooc.imooc_voice.view.loading.LoadingActivity

I started here is LoadingActivity, is SplashActivity, you can see there are three terminal Time:
Run Log
This Time: Activity last start time.

Total Time: A series of Activity start time.

Wait Time: Total start-up time, the system comprising a cold start, app information needs to be loaded into memory.


In this one, we are able to optimize Total Timethis part, provided that you use the time you start feel too long. So how do we know in the end is a piece of code, or say which of the three parties sdk initialization when loading time is too long, then you need to know 代码执行时间统计的方法.


2. code execution time statistics

1.我们在自己的 Application 文件中 加入以下代码,这是我自己项目中的 Application 文件
public class ImoocVoiceApplication extends Application {

    private static ImoocVoiceApplication mApplication = null;

    @Override
    public void onCreate() {
        super.onCreate();
        File file = new File(Environment.getExternalStorageDirectory(),"app.trace");
        Debug.startMethodTracing(file.getAbsolutePath());
        
        mApplication = this;
        //视频SDK初始化
        VideoHelper.init(this);
        //音频SDK初始化
        AudioHelper.init(this);
        //分享SDK初始化
        ShareManager.initSDK(this);
        //更新组件下载
        UpdateHelper.init(this);
        //ARouter初始化
        ARouter.init(this);
        
        Debug.startMethodTracing();
    }

    public static ImoocVoiceApplication getInstance() {
        return mApplication;
    }
}

2.我们通过命令获取 trace 文件
adb pull /storage/emulated/0/app.trace

3.将 trace 文件 直接拖到 Android Studio 中,如下图所示

tarc file analysis chart

As can be seen from this chart below, my own projects takes longer is this ARouterinitialization and ShareSDKinitialization, I chose here Top Downto view the next module, I think the most convenient, you can click on the module to view this see personal habits.

Now that you know the problem, how to optimize it, here we are given two options:

1. The initialization is here performed in the main thread, the thread may be treated with an asynchronous manner.
  • Note: If you put some initialization on the asynchronous thread, then the initialization code 不能有创建 Handler 的操作, 不能有 UI 操作while this method is limited to 对异步要求不高that you optimize the initialization time not too long in asynchronous mode, this is to prevent the event your data may need to wait until the home screen Applictionto get to the initialization interface is complete, and you will be part of the initialization operation on the asynchronous thread, which may result in data acquisition less.
2. lazy loading.
  • Some developers like in Applicationthe initialization OKHttp, which is more time-consuming, it can be used to solve lazy loading, that is used to initialize the go OKHttp.

Here I offer these two ideas, but the specific details still have to optimize the operation of the project according to you own needs as well as to optimize the practical application. Of course, if you have a better idea of ​​optimization, welcome to discuss, together we progress together, grow together.



Written at the end of the text

Paper come Zhongjue know this practice is essential. "Winter reading shows sub Yu" - Lu

App startup time optimization on the code of the program to achieve when it comes to this, we can according to the protocol given in the article, in conjunction with their project to optimize.


Codeword is not easy, even if this article you have a little bit of help, please do not mean your point of praise, I will continue to bring more quality articles.

Published 11 original articles · won praise 12 · views 7747

Guess you like

Origin blog.csdn.net/wild_onlyking/article/details/104702308