[Android] APP startup optimization study notes

Start optimization purpose

user experience:

The startup speed of the application directly affects the user experience. Users expect apps to launch quickly and respond quickly to their actions. If an app launches slowly, users may become dissatisfied and may choose to uninstall or switch to a competitor's app. Through startup optimization, the startup speed of the application can be improved, allowing users to obtain a better experience.

Competitive Advantage:

The mobile application market is highly competitive, and users have a huge number of choices. If your app launches slowly, you may lose users, especially when compared to similar apps. By enabling optimization, you can improve the response speed of the application, attract more users to stay in the application, and improve the differentiation competitiveness from competitors.

Resource management:

When the application starts, it may need to load and initialize many resources, such as interface layout, data, etc. If the startup process is lengthy and consumes too many resources, it may increase the system burden, affect the performance of other applications, and even cause the system to crash. By starting optimization, you can manage and utilize resources more effectively, reduce the occupation of system resources, and improve the overall performance and stability of the system.

Start the scope of optimization

During the startup process of an Android application, there are three main screens, which are the first screen, the second screen and the third screen. The first screen refers to 应用程序加载的过程a background image that is usually displayed in a theme. The second screen means Splash页面that the content of the startup interface is displayed. The third screen refers to MainActivitydisplaying the main interactive interface of the application.

In order to improve the user experience, we usually pay attention to the time it takes for the application to start to an interactive page, that is, the time spent on the first screen and the second screen. Before that, there is the creation process of Application and the startup creation process of SplashActivity. Therefore, within the scope of optimization of the startup, it will usually be from Application.attachBaseContext()the beginning until Main.onWindowFocusChanged()the end.

The goal of optimization is to reduce the startup time and allow the application to display interactive pages as soon as possible. Therefore, it is necessary to pay attention to the time consumption of the first screen, that is, the startup time of the Application, and the total time consumption of the first screen and the second screen, that is, the startup time from the Application startup to the interactive page.

The call sequence of the application creation process:
insert image description here
the call sequence from the creation of the Activity to the display process
insert image description here

Optimization ideas

1. Tool use

insert image description here
insert image description here
insert image description here
In this way, you can use the performance detection tool brought by Android Studio for detection.

2. Optimization method

1. Lazy loading, including lazy loading of business and layout
2. Throw it to the child thread to load it by itself
3. Load in advance, improve efficiency through multi-threading
4. Check the IO operation of the main thread
5. Control the number of threads and the number of GC

Application.onCreate()

SDK optimization:

Lazy loading:

Delay the initialization of the SDK until it is really needed, instead of loading it all during the initialization of the Application. This reduces initialization time at startup and improves startup speed.

Optimization of dependencies between SDKs:

Some SDKs may need to be initialized after other SDKs have finished loading. Topological algorithms can be used to optimize the loading order of SDKs to ensure that dependent SDKs are loaded and initialized before dependent SDKs. This avoids initialization errors or time consuming due to dependencies.

The child thread initializes the SDK:

Put the initialization process of the SDK into the child thread to avoid blocking the time of the main thread. In particular, some SDKs that must be initialized can be loaded through multiple threads and use CountDownLatch to block and wake up threads to ensure that the main thread can respond to user operations as soon as possible.

Business code optimization

Misuse of ContentProvider is prohibited:

Since the ContentProvider is initialized at Application.attachBaseContext(), abuse can increase startup time. Therefore, you need to use ContentProvider carefully and avoid using it too much to handle business logic.

Optimize time-consuming operations on the main thread:

Find and optimize time-consuming code in the Application main thread to reduce the impact on startup time. Time-consuming operations can be moved to sub-threads through asynchronous operations or other optimization methods, thereby improving the startup speed of the application.

Activity.onCreate()
Optimize layout:

Reduce repeated rendering of layout backgrounds, reduce layers, and use ViewStub to load layouts that are not necessarily displayed on demand. You can consider loading the layout in the child thread or loading it in advance to reduce the blocking time of the main thread.

Lazy initialization:

Avoid time-consuming operations in initView(), such as IO operations, players, etc. Make these operations lazy-loaded on demand, or load them in multiple threads to avoid blocking the main thread.

Lazy loading Fragment:

For the ViewPager+Fragment of the main page, the Fragment is lazily loaded on demand, and only loaded and initialized when sliding to the corresponding page, avoiding loading all Fragments at one time, saving resources and improving response speed.

Activity.onResume()

Don't do time-consuming operations here, don't do time-consuming operations here, don't do time-consuming operations here.
Knowledge points in WMS: page rendering is in onResume.

insert image description here

Optimization of dependencies between SDKs (continue to the second point of SDK optimization above)

Directed Acyclic Graph (DAG)

A graph is a data structure consisting of a set of nodes (also called vertices) and the edges connecting those nodes. A directed graph means that the edges in the graph have a direction, while the edges of an undirected graph have no direction. Directed acyclic graph means that there is no path forming a loop in the directed graph, that is, starting from any node and passing through several edges cannot return to the starting point.

A common scenario for using DAGs in app launch optimization for Android is dependency management. In complex Android applications, there may be dependencies between different components (such as Activity, Fragment, Service, etc.), that is, the startup of a component depends on the completion of other components. With the help of a directed acyclic graph, these dependencies can be clearly represented, and the startup sequence can be determined through a topology sorting algorithm to improve the startup performance of the application.

Specifically, Android's application startup process can be viewed as a directed graph, where nodes represent different components, and edges represent dependencies between components. For example, an Activity may need to wait for other Activities to complete initialization before starting. By constructing an effective directed acyclic graph, these dependencies can be visualized, and the startup order can be determined according to the topology sorting algorithm, thereby reducing unnecessary waiting time and improving the response speed of the application.

Android Startup

Android Startup provides a simpler and more efficient way to initialize components when the application starts. Developers can use Android Startup to simplify the startup sequence and explicitly set the initialization order and dependencies between components. At the same time, Android Startup supports synchronous and asynchronous waiting, manual control of dependent execution timing, and ensures the initialization order of internal dependent components through directed acyclic graph topology sorting.

Link: A big article about Android Startup

Guess you like

Origin blog.csdn.net/qq_43358469/article/details/132017609