Optimization of starting speed of Android actual combat APP

  • 2021-09-05 01:00:52
  • OfStack

The startup speed of APP is very important, and the slow startup speed of APP may cause poor user experience. Especially after using Android studio recently, if app is not turned on for a long time, the startup speed will be particularly slow. Let's discuss the reasons that affect the startup speed of app and the solutions.

Detect startup time

First of all, we need to know the startup time of app, and then you can also feel it. Here I teach you a way to pretend to be forced:


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

The startup time can be detected with the adb command, as shown below:


./adb shell am start -W "com.hchstudio.dict"/".MainActivity"
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.hchstudio.dict/.MainActivity }
Status: ok
Activity: com.hchstudio.dict/.MainActivity
ThisTime: 7524
TotalTime: 7524
WaitTime: 7551
Complete
com. hchstudio. dict is the application package name . MainActivity is the main page function to start WaitTime is the startup time we are concerned about

So does 7551 take a long time to start? The answer is very long. . . This is an app that has not started for a long time. When it starts for the first time, it has a long starting time. Let's talk about how to reduce it.

Reduce application startup time

The startup process of app, simply speaking, mainly includes two parts:

Application onCreate method for starting interface

Therefore, the simplest way is to minimize the time-consuming operations in these two methods.

The above method solves the startup speed from the code, but the user still feels slow when starting. What should I do? Directly on the code:


<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorAccent</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.Launcher">
<item name="android:windowBackground">@color/colorLauncher</item>
</style>

This is android style file, in which AppTheme. Launcher is the theme style of the startup interface. Add android: windowBackground to your app homepage theme style, and put a background picture of app, so that even if app starts slowly, the background will be loaded first, which will give users an illusion that app has started.

Instant Run

What do you know about the new feature of Android studio Instant Run?

Instant Run is also one of the factors affecting startup, but this is not available in the released version, so don't worry.


Related articles: