One, foreword

APP optimization is the only way for us to advance senior development engineers, and the optimization of APP startup speed is also the first step for us to open APP optimization. When users use our software, the most frequent interaction is the startup page of the APP. If the startup page is loaded too slowly, users may have a bad impression on our APP, thus consuming their patience, and even worse, users may uninstall the APP. This is also the reason why wechat always insists on using “a little man looking at the earth” as the background of the launch page, and insists on not adding the launch advertisement.

2. Three startup modes of APP

Take a look at Google’s official documentation, Launch-Time Performance, for an overview of app Launch optimization.

Application startup can be cold startup, hot startup, or warm startup, and cold startup is the slowest and takes the longest time.

Cold Start

When an application starts, the system creates a new process and assigns it to the application if no process of the application is running in the background (for example, the process is killed or started for the first time).

Hot Start

This is the process of starting an application from an existing process. In general, it is the process of using the back or Home button to return to the system home screen and re-start the Activity through the latest task. Less overhead than cold start.

Warm Start

There are many scenarios for warm start. Common examples are: 1. The user uses the back key to exit the application and then immediately restart the application. 2. The application is cleared from memory. If you open the application again, it will be restored using the instance state saved in OnCreate().

Cold boot is the process of starting the APP from scratch, while the other two boot methods are a process of returning the APP from the background activity to the foreground. There is no clear distinction between hot start and warm start, let’s call them collectively hot start. In the development, we pay more attention to the optimization of cold start. This paper also analyzes the startup process of cold start from the example of Android, and gives the optimization scheme of cold start.

3. Cold startup process of APP

When cold start starts, the system will perform three tasks in sequence to start the APP:

  • Load and start the application
  • Immediately after the APP starts, create a blank startup Window
  • The process of creating the APP

After these three tasks are executed, the system creates the application process, which then performs the next step:

  • Creating an APP object
  • Start a main thread
  • Create the Activity for the launch page
  • Load the View
  • Layout view to screen
  • Perform initial drawing to display the view

After the application process completes its initial drawing, the system process replaces the currently displayed blank Window with the Activity of the launch page, at which point the user can use the App.

Iv. APP startup time

The startup time of the APP is the basis on which we can verify the optimization effect. The startup time refers to the period between opening the APP and initialization and displaying the Activity on the startup page. APP Startup Time

You have used logcat to view the startup time

On Android versions above Android4.4 (API Level 19), Android Studio automatically outputs the startup time in Logcat when an app is launched. This time is calculated from the start of the application (the creation process) until the first drawing of the view (that is, the Activity content is visible to the user) is completed.

For example, Display:

I/ActivityManager: Displayed com.example.app/com.example.app.SplashActivity: +1s742ms (total +49s450ms)
Copy the code

ReportFullyDrawn () method

The Activity’s reportFullyDrawn() method, It prints in Logcat how long it took from apK initialization until the reportFullyDrawn() method was called. (The reportFullyDrawn() of the article is executed in onCreate() in SplashActivity, You can see that the Displayed time is the same as the Displayed time.

I/ActivityManager: Fully drawn com.example.app/com.example.app.SplashActivity: +1s742ms (total +49s450ms)
Copy the code

Run adb to manually check the startup time

adb shell am start -W [packagename]/[packagename.SplashActivity]

Copy the code

Output:

Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.app/.SplashActivity }
Status: ok
Activity: com.example.app/.SplashActivity
ThisTime: 1368
TotalTime: 1368
WaitTime: 1432
Complete

Copy the code

ThisTime: how long it took for the last Activity to start;

TotalTime: the startup time of all your activities;

Just focus on TotalTime

Start time standard

Officially, when the startup time exceeds the following indicators, the startup time will be considered too long, which requires careful optimization of the startup time.

  • The cold start time exceeds 5s
  • The hot start time exceeds 1.5 seconds
  • Warm start time exceeds 2s

Said a lot of basic knowledge of the start, below began to explain the real optimization of the actual combat

Five, start optimization of the actual combat

Solve the application startup blank screen problem

As mentioned above, application initialization will carry out a series of process creation and resource initialization. During this period, the system will allocate a blank Window first, which will cause the user to open the application and display the first interactive Activity, and experience a blank screen for a period of time.

In this case, we can define a theme for the app to solve, and display a theme background before the Activity is displayed, to fill the empty Window stage.

Define a Splash theme

<! --Splash launcher--> <style name="LauncherTheme" parent="AppTheme">
        <item name="android:windowBackground">@mipmap/ic_splash_bg</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
Copy the code

Reference this topic in the manifest

 <activity android:name=".activity.SplashActivity"
            android:theme="@style/Launcher">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
Copy the code

Finally, remember to restore the default APP theme after the launch page is displayed

  override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTheme(R.style.AppTheme)
        setContentView(R.layout.activity_splash)
      
  }
Copy the code

This method only visually gives the user a sense of fast startup and does not reduce the actual startup time.

Avoid excessive Application initialization

As our project grew, third-party libraries and components became increasingly dependent on our project, inevitably performing a lot of third-party library initialization in the Application’s onCreate(). The heavy initialization effort makes the life cycle too heavy and can lengthen the startup time of the application, so we should sort and optimize these third-party libraries.

  • Must be initialized in onCreate() and in the main process
  • It can be deferred, but it needs to be initialized in the Application
  • Initialization can be deferred to the start page’s lifecycle callback
  • Delay initialization until needed

You can clean up your code according to your project. If you can delay execution, you should initialize it in IntentService or Work Thread.

  • For example, If EventBus needs to be used in Activiy, it must be initialized in Application
  • Libraries such as Bugly, GrowingIO, and others can be initialized in the Work Thread
  • For example, map positioning, ImageLoad can be deferred until initialized before use
  • Network-loaded resources in SplashActivity can be loaded for the first time and stored in the cache until the next startup
  • Note that some third-party libraries must be initialized in the main thread
  • Avoid time-consuming operations, such as database I/O operations, on the main thread
  • Remove useless or duplicate code
  • Reduce network request density in the first screen Activity