The performance optimization of starting speed and lag is a complex multi-directional optimization, which requires us to keep practicing and summarizing experience in the project. This article is just a brief introduction to related concepts, tools and directions for optimization, etc.
In fact, the optimization of startup speed, lag and layout are all linked, each aspect of the optimization will more or less affect the other aspects. And some details of these aspects of the optimization is actually a tedious operation, also need our understanding of the project has greatly, and optimized the many small aspects after may effect is not significant, this is very normal, we should not compare the absolute time, but should compare and project before time, is there a small optimization, These small changes can add up to a lot;
First, start speed optimization
1. Start state
Cold start: The cold start indicates that the application process does not exist. You need to start the application process before starting the Activity.
Warm start: A warm start is when we exit the application and then restart the application. If the application process is not killed, we do not need to restart the process, but just start the activity.
Hot start: Hot start refers to the application being switched to the background, and the process and activity are not killed in memory. You can switch from the background to the foreground without restarting the process and activity.
2. Statistics of startup time
Methods a
The first method can be displayed in the Logcat log screen of Android Studio. Enter the search keyword displayed and select No Filters. When the application is started, the following log will be printed, indicating that the startup time is 887 milliseconds.
Active: Displayed com.xiangxue. Arch_demo /.mainActivity: Displayed com.xiangxue. +887ms
Method 2
To view the second method, run the following command on the CMD command line interface:
adb shell am start -S -W [packageName]/[activityName]
The output is as follows, where TotalTime is the elapsed time;
C:\Users\29155>adb shell am start -S -W com.xiangxue.arch_demo/.MainActivity
Stopping: com.xiangxue.arch_demo
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.xiangxue.arch_demo/.MainActivity }
Status: ok
LaunchState: COLD
Activity: com.xiangxue.arch_demo/.MainActivity
TotalTime: 887
WaitTime: 890
Complete
Copy the code
3. Start speed analysis
To analyze startup speed, we can use Android Studio’s built-in profiler;
- Run — Edit Configurations — profiling — start this recording on startup
- There are mainly two modes to choose from: Sample Java Methods: Sampling at intervals, sampling at intervals to analyze the time occupation of corresponding methods; Trace Java Method: Method call tracing, which tracks each method call and then records the time spent on each method. This method is cumbersome and not necessary.
- After saving, we can start the app in profile mode and view the information about startup time in the profiler interface.
- The prfiler interface mainly looks at three modules to analyze the startup time
Top Down: method call stack information. By analyzing the time consuming of each method in the process of method call stack, we can determine which process consumes more time. Flame Chat: Also known as Flame chart, it is a time bar to record the time consuming of a method. We usually use this chart to review the time consuming of each method. To really analyze the time, we have to analyze the specific method in the Top down. “Bottom Up” : this is the same method call information, but the reverse of the Top Down, from the Bottom to the Top, usually do not look at the module, look at the first method call stack.
4, black and white screen problem
When we click the app icon, there will be a white screen or a black screen when the app is not started. This is the interface loading method provided by Google, but the user experience is not good. We can solve this problem by setting the background image.
We first define the style property and set the WindowBackground property to our background image.
Then modify the theme property in the Activity tag, using our custom style tag.
Finally, we need to setthe setTheme in the activity onCreate method to return to the application theme, because we still need to use the application theme after launching.
Adding background images does not optimize the startup speed, but only improves the user experience.
<resources> <! -- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <! -- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="AppTheme.myapp"> <item name="android:windowBackground">@drawable/ic_controls</item> </style> </resources> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:name=".application.ArchDemoApplication" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:theme="@style/AppTheme.myapp"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> protected void onCreate(Bundle savedInstanceState) { setTheme(R.style.AppTheme); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ,,,,}Copy the code
2. Caton analysis
The operation of Caton analysis is basically the same as that of startup speed analysis, which is also analyzed by Android Studio’s own profiler.
- Open the Profiler screen and keep the application running on the device or emulator.
- Click THE CPU running module to enter the CPU analysis interface;
- Select Trace System Calls in The Select CPU Profiling Mode and click Record;
- Then we apply the interface we want to analyze on the device, and when we’re done, click Stop;
- The SystemTraceRecording page is automatically generated.
We can analyze in the generated interface, the interface shows a lot of useful information, such as the elapsed time of each frame, if the time is too long it will turn red, and the specific method name is displayed in the following time bar;
You can also view the specific method call stack information in the Top Down screen on the right; It is the same as the right screen of startup speed optimization;
Third, layout analysis
Layout optimization can be analyzed from three aspects;
1. Hierarchical optimization
When the framework layer conducts measure, layout and draw operations on the layout, it calls the layout recursively layer by layer. Therefore, the deeper the layout hierarchy is, the longer it takes. We should minimize the layout hierarchy and flatten the layout.
You can use the Layout Inspector that comes with Android Studio to analyze the Layout.
- Running our application on a device or emulator;
- Tools: LayoutInspector Displays the layout analysis screen. The system automatically detects and displays the application process.
- View the view hierarchy in the left view tree interface. View view properties on the right property interface;
Use the Merge tag
If you have layouts that need to be used in more than one place, you can separate them into a single layout file and include them where you need them. This type of layout uses merge tags. The merge tags are automatically removed when the layout is appended to another layout. Instead, you wrap them with layout tags, which creates a layer of redundant layout nesting;
Use the ViewStub tag
When a view or viewgroup in the layout is only displayed under special circumstances, we can use the ViewStub tag. It is invisible and consumes no resources, and is initialized only when we set it to Visible or call the Inflater method.
2. Overrender
We can check for overdrawing by turning on the Render option in Developer mode; Overdrawing is generally solved by considering the following ways;
- Remove unwanted backgrounds from the layout
- Make the view hierarchy as flat as possible
3. Layout loading optimization
Asynchronous loading can be done asynchronously using the libraries in androidX for layouts
Implementation "androidx asynclayoutinflater: asynclayoutinflater: 1.0.0."
new AsyncLayoutInflater(this) .inflate(R.layout.activity_main, null, new AsyncLayoutInflater.OnInflateFinishedListener() { @Override public void onInflateFinished(@NonNull View view, int resid, @Nullable ViewGroup parent){ setContentView(view); / /... }});Copy the code