Preface:

The previous two articles are mainly about the activity, fragment lifecycle summary, this article mainly summarizes the optimization scheme in the Android application development process, and some commonly used optimization tools. The optimization direction includes: start speed, interface fluency, memory usage, APK volume, power consumption, flow, and so on.

App startup speed

1. Use style to set a default startup diagram to improve the startup speed from the interactive experience

2. Analyze application and first screen business logic to asynchronously initialize third-party components to prevent blocking the main thread (or delay initialization (initialize when used)).

The 2 second pause of the splash screen can be used to delay some time-consuming operations to initialize here

4, with the tool DDMS TraceView to detect the point of time, do specific processing

MainActivity’s onCreate process, especially UI layout and rendering, can cause serious startup performance problems if the layout is too complex. (Consider requesting the data mainActivity needs first), and consider lazy loading depending on the structure of the home page.

The Android APP launch optimization: wuxiaolong. Me / 2017/03/13 /…

App launch speed optimization of time-consuming detection processing: www.jianshu.com/p/a0e242d57…

Use TraceView find caton culprits: blog.csdn.net/u011240877/…

The above articles basically describe the app startup process, how to optimize the white screen, detect time consuming, lazy SDK loading, etc…

Interface fluency

1. When it comes to UI fluency, it generally means not doing time-consuming operations in the main process, and improving UI drawing speed (reducing the layout hierarchy of views, avoiding transition drawing, etc.)

2. Rational use of merge, include, and ViewStub labels reduces the layout hierarchy

3. Do not do time-consuming tasks in the custom View’s OnDraw

The Android UI performance optimization of actual combat: blog.csdn.net/lmj62356579…

Performance optimization of layout optimization: www.trinea.cn/android/lay…

Of course, for UI stuttering, it is inevitable to introduce a detection scheme:

Method 1: Generally, Looper logs are monitored

Way 2: Use Choreographer

There are also some open source tools:

Github.com/markzhai/An… Way [1]

Github.com/wasabeef/Ta… Way [2]

Github.com/friendlyrob… Way [2]

Memory optimization

Memory optimization is mainly to eliminate memory leaks and avoid memory jitter in the application.

1. Android Studio memory analysis tool + Mat can detect memory jitter and memory leak very well

2, common memory leaks:

● Singletons: long life cycle, reference to a variable with short life cycle, resulting in unreleasable. For example, activity leak

● Static variables: Also because the life cycle is longer

● Memory leaks caused by non-static inner classes creating static instances

If the handler is declared static, its survival is not related to the lifetime of the Activity. Introduce the Activity by soft reference.)

Anonymous inner classes (anonymous inner classes refer to external classes and cannot be released, such as various callbacks)

● Resources are not closed (BraodcastReceiver, ContentObserver, File, Cursor, Stream, Bitmap)

● Reuse problem (Bitmap release)

Android memory optimization & practice: mp.weixin.qq.com/s/2MsEAR9pQ…

OOM: Hukai. Me/Android…

The Android memory leak analysis, improving the experience summary: zhuanlan.zhihu.com/p/20831913

Apk volume optimization

Code thin body

● Remove useless code and functionality;

● Remove useless libraries and avoid libraries with similar functions;

● Enable Proguard;

● Reduce the number of methods;

● The third party open source library thin, only retain their own part of the need

Resources thin body

● Remove useless resource files;

● Drawable directory only keeps one resource;

Compress the picture;

● PNG to JPG;

● Use vector drawings;

● Use WebP;

● Resource confusion;

● Online resources;

● Can not use the picture of the picture, without the realization of the code

So thin body

● Retain architecture-specific So for user model distribution where permitted;

Power consumption

Power is a very valuable resource for mobile devices, and as a developer, it is necessary to reduce the power consumption for the sake of the user. Surveys show that only about 30% of the power is consumed by the core functions of the application, such as interface rendering, and the remaining 70% is consumed by reported data, location updates, and background notifications.

How is it tested?

1. Check the stats of APP power consumption in the phone options

2. Use Battery Historian Tool to see detailed electricity consumption

How to optimize

● Reduce the number and duration of screen awakenings and use WakeLock correctly.

● Delay non-essential operations to the charging state, such as log reporting can be completely completed during nighttime charging, which can be used in conjunction with JobScheduler

● When using sensors to collect data, once there is no need to remember to cancel registration.

● Reduce network communications, combined communications.

● Reasonable use of positioning function, reduce the frequency of position update and according to the actual situation using different precision positioning needs

Network optimization

Now almost all apps need to be connected to the Internet. Network optimization can improve the experience on the one hand, and reduce the loss of traffic and power on the other hand. In addition, the network is also a resource for both users and isPs, and no developer should assume that network resources are unlimited

How to detect

● Use the Network Traffic Tools in Android Studio to view Network requests

● Android Studio3.0’s new performance analysis tool is easier to use with Monitor in Android Studio

● Analyze network packets using a packet capture tool such as Fidder or Charles

How to optimize

● Be sure to do a good job of caching when necessary, whether it is images or ordinary data, using LruCache and DiskLruCache to build their own caching system, and according to the actual scenario design caching strategy

● Avoid excessive network synchronization and merge related network requests

● According to the actual scenario to determine the request policy, avoid using a fixed interval frequency for network operations. For example, when WiFi is connected and charged, the request frequency can be high. After the first network request fails, a double time interval can be used for the next one

● Reduce the amount of data transmission, the transmission of data to do compression. If you are transferring images, you need to select the appropriate image format and request the appropriate size of the image based on the display size. For normal data, you can consider using ProtocalBuffers to reduce the size of the transmitted data.