For technical practitioners, many times it’s not that they don’t know how to do it, but that they don’t know what to do? Today I have collected some experiences on how to optimize Android applications. , a total of eight dimensions.
#1. Layout optimization
Why?2. Memory optimization
Reasonable use of multiple processes, such as the music player class, can be divided into the main process and the playback process
Consider bounded queues when using asynchronous queues
If you can clearly know the size of HashMap, then set the capacity for it during initialization
3. Battery Optimization
Battery is a very precious resource for mobile devices. As a developer, you have It is necessary to consider the user and reduce power consumption. Surveys show that usually only about 30% of the power is consumed by the core functions of the program, such as interface rendering, and the remaining 70% is consumed by reporting data, location updates, and background notifications .
View the APP’s power consumption statistics in the phone options
Use Battery Historian Tool to view detailed power consumption
Reduce the number of times you wake up the screen With the duration, use WakeLock correctly.
When delaying non-essential operations to the charging state, such as log reporting, it can be completed during night charging. This can be used in conjunction with JobScheduler
When using sensors to collect data, remember to cancel the registration once it is no longer needed.
Reduce network communication and merge communication.
Use the positioning function rationally, reduce the frequency of location updates and use different precision positioning requirements according to the actual situation
4. Network Optimization
Nowadays, almost all Apps need to be operated online. Network optimization can improve the experience on the one hand, and improve the experience on the other hand. Reduce traffic and power consumption. In addition, the network is also a resource, whether for users or network service providers, and no developer should assume that network resources are unlimited.
Use Network Traffic Tools in Android Studio to view network requests
Use Monitor in Android Studio
Use packet capture tools such as Fidder or Charles to analyze network data packets
Be sure to cache when necessary, whether it is pictures or ordinary data, use LruCache and DiskLruCache to build your own caching system, and design caching strategies based on actual scenarios
Avoid excessive network synchronization and merge related network requests
Determine the request strategy according to the actual scenario and avoid using fixed interval frequencies for network operations. For example, when connecting to WiFi and charging The frequency can be high. After the first network request fails, double the time interval can be used for the next one.
Reduce the amount of data transmission and compress the transmitted data. If the transmission is For pictures, you need to choose the appropriate picture format and request pictures with appropriate specifications according to the display size. For ordinary data, you can consider using ProtocolBuffers to reduce the size of the transmitted data.
In some cases IP direct connection can be used, which on the one hand can reduce DNS resolution time and on the other hand can prevent domain name hijacking
5. Start optimization
in onCreate
Use adb shell am start -W [packageName]/[packageName.MainActivity]
Measure cold start time
Reduce complex and time-consuming operations in onCreate() of Activity
Application’s onCreate(), attachBaseContext () also reduces complex and time-consuming operations, but for many apps, a large number of initialization operations of components and services will be performed here. If possible, consider parallel initialization
Provide customized startup Window, such as displaying a picture as a startup window by setting a theme.
Optimize layout
6. Volume optimization
For users, whether it is user space or network, or Time is a resource. Volume optimization is an important part of saving resources for users. If you are currently making SDK products, then volume optimization is equally important.
Use Android Lint to check unused resources
Reduce unnecessary dependent libraries/Jar, and give priority to small ones while meeting the needs.
Use the Proguard tool for code slimming, optimization, and obfuscation
Reduce the number of so files and provide so files according to the actual situation
Use shrinkResource in Gradle to exclude useless code and resources from the APK installation package
Reduce the size of image resources, consider image compression or use Vertor Drawable instead of png/jpeg
Selectively provide images with corresponding resolutions Resources
Reuse existing images, mostly by transforming existing images through code
Use plug-in technology (Don’t use it if the project is simple)
7. Performance optimization
can be used If you give 100% of your capabilities, don't only use 50% of them. This is not a bad thing for the application. If two cars are sold to users at the same price, I think most people will choose the one with better performance.
Use Lint to perform static analysis, in Android Studio's Analysis->Inspect Code
Turn on StrictMode in the developer options or turn it on in the code
Code Review
Task parallelization, perform parallel operations on possible tasks, use thread pools instead of directly using threads
How to serialize data, give priority to Android Serializable provided by itself rather than provided by Java
Choose the appropriate data structure and clarify the complexity of List/Set/Map/Stack operations
Use Android to provide more efficient containers, such as using ArrayMap instead of HashMap. In addition, there are SparseBoolMap, SparseIntMap, SparseLongMap
Since the business of each product is not the same, it is difficult to have a general optimization plan. Here are two more goals worth thinking about:
If possible, serial business Parallelization
If possible, simplify the business process. The way to put an elephant in the refrigerator is to open the refrigerator, put the elephant in, and finally close the refrigerator.
The fundamental reason why business optimization is put last is that business optimization has high risks and requires the overall cooperation of the team to complete.
There are four color formats for pictures in Android, which are
##The default is ARGB_8888<span style="font-size:14px;"></span>
, where ARGB represents transparency, red, green, and blue respectively. Each value is recorded with 8 bits, that is, one pixel will occupy 4byte, a total of 32 bits. AndARGB_4444<span style="font-size:14px;"></span>
is very similar to the above, but each value is recorded with 4 bits, that is, one pixel will occupy 2byte, a total of 16 bits . RGB_565<span style="font-size:14px;"></span>
uses 5 bits, 6 bits, and 5 bits to record each value. There is no transparency, and each pixel will occupy 2 bytes. 16 bits in total. ALPHA_8<span style="font-size:14px;"></span>
: This pixel only saves transparency and will occupy 1byte, 8 bits in total. In practical applications, it is recommended to use ARGB_8888<span style="font-size:14px;"></span>
and RGB_565<span style="font-size:14px;"></span>
, if you don’t need transparency, then choose RGB_565<span style="font-size:14px;"></span>
, which can reduce the memory usage by half.
Android video tutorial]
The above is the detailed content of For Android application optimization encyclopedia. For more information, please follow other related articles on the PHP Chinese website!