1, the background,

Recently, I developed a new App. The construction period was tight in the early stage and the work was relatively extensive. After the launch, I found that the startup time of the App was relatively long, reaching 3 seconds.

Start a white screen, the experience is not good, this can only be optimized later, it is best to consider the early development

2, research

2.1. Startup mode in Android

1. Cold start: If there is no application process in the background when the App is started, the system will create a new process and assign it to the App. This startup mode is called cold start

2. Hot start: If the App starts with the application process in the background, the system will directly use the existing process

From the meaning of the two startup modes, it can be seen that cold startup is time-consuming, hot startup is generally faster, and the startup optimization we want to do is generally cold startup optimization

2.2. Cold start process

The system creates the application process time we generally cannot optimize, this is the system creates the simple process

Where we can optimize

1. Application 2. SplashActivity 3

2.3. Startup time

1. Run the command

adb shell am start -W [packageName]/[packageName.MainActivity]
Copy the code

At this point, if the activity is launched without setting export:true in its Manifest, it may not be opened by command

2. Bury them yourself

As can be seen from the above startup process, Android cold startup requires the system to create a process and then create an Application. During the process of creating a process, we can start burying points in the attachBaseContext method of the Application

/** * Collects statistics on the cold start time */
public class StartLogHelper {

    private static final String TAG = "StartLogHelper";
    private static long startTime;

    /** * Set the start time temporarily to the start time in the attachBaseContext method of the Application@param start* /
    public static void setStart(long start){
        startTime = start;
    }

    /** * Application initialization time, put at the end of the application onCreate method */
    public static void getApplicationTime(){
        long end = System.currentTimeMillis();
        DebugLog.e(TAG,"==================getApplicationTime==" + (end - startTime));

    }

    /** * Welcome page initialization time */
    public static void getWelcomeTime(){
        long end = System.currentTimeMillis();
        DebugLog.e(TAG,"==================getWelcomeTime==" + (end - startTime));
    }

    /** * Main page initialization time */

    public static void getMainTime(){
        long end = System.currentTimeMillis();
        DebugLog.e(TAG,"==================getMainTime==" + (end - startTime));
    }
Copy the code

3, in the studio logcat log

Enter the application package name in the Logcat log

Then you can see the time of each stage

3, plan

From the principle of research cold start, we can figure out what we need to optimize

1. Blank screen during cold startup

During the cold startup, we found that there would be a white screen. A white screen would appear first, and then the UI of the welcome page would appear. This is related to the background of the system setting, windowBackground

Solution:

Set a custom theme property on the welcome page

SplashActivity manifest file Settingsandroid:theme="@style/SplashScreenTheme "SplashScreenTheme" parent="Theme.AppCompat.NoActionBar"> android:windowBackground">@drawable/bg_splash  xml version="1.0" encoding="utf-8"? > http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" />
        <bitmap
            android:antialias="true"
            android:gravity="center"
            android:src="@drawable/bg_show" /></item> </layer-list> where the image of bg_show is the same as the initial image of the welcome page, so that the style of the launch of the white screen is the same as that of the welcome page, giving the impression that there is no white screenCopy the code

2. Optimization of startup time

We can tell from the boot time above

1. It takes time to initialize the third-party SDK in Application onCreate

public class AppConfigHelper {
    public void setAppConfig(Application application){
        addSyncTask(application);
        addAsyncTask(application);
        TaskConfigManager.getInstance().startInit();
    }
    
    
    private void addSyncTask(Application application) {
        TaskConfigManager.getInstance().addTask(new ZMLTask("oneLogin") {
            @Override
            public void run(){ JVerifyHelper.init(application); }}); }} put together what needs to be executed synchronously in the Application and what can be executed asynchronouslyCopy the code

2. Homepage time-consuming method

When we arrived at the home page, the time we buried was less than the time the user saw the data. This was because the home page loaded a lot of data. We loaded 7 fragments on the home page at the same time and requested more than 10 interfaces at the same time

1. Lazy loading of the home page ensures that only about 3 App startup interfaces are requested

2, the home page UI optimization, while showing the UI after the implementation of the logic processing

3. Do delayed loading of unimportant logic on the home page, such as request upgrade popup