Opening words A while ago, my boss gave me a task, which was to help us reduce the size of the Android installation package of the live broadcast application we developed. It took me about a week to reduce the installation package from 18MB to 12.5MB. It could have been optimized to less than 10MB, but due to other reasons, it is currently only 12.5MB. Here I record the optimization ideas and tools used for my own review in the future, and for reference by friends in need. Purpose of weight loss From the perspective of purpose orientation, we will not do something for no reason. So what is the purpose of our application slimming? The answer is: to improve the download conversion rate. What is the download conversion rate? For example: your application size is 18MB, and there are 100 potential users who want to download and try it. As a result, 20 users dislike the installation package is too large and leave directly. 20 users cancel the download while waiting for the download. In the end, only 60 users actually download and install. Then the download conversion rate of the application is 60/100 = 60%. The simple summary is: the smaller the installation package, the shorter the waiting time for users to download, the better the experience for devices with small mobile storage configurations, and the higher the download conversion rate of the application. I remember hearing a WeChat expert say in Tencent Lecture Hall that the first version of WeChat was only about 400KB, and I was instantly in awe. Installation package composition To slim down the installation package, you first need to understand the composition structure of the installation package. Here is a brief summary of the components and their functions: Among them, the installation package accounts for a large proportion of: dex files, res folders, assets folders, lib folders and resource.arsc files. Therefore, the next slimming optimization is to make these files smaller to achieve the purpose of slimming. Starting from Android Studio 2.2.3, the function of browsing APK structure has been added. We can directly drag the installation package into the IDE and browse its composition and corresponding size directly, so that we can easily compare and analyze the results of each optimization step. Resource reduction After understanding the composition of APK, we can start to optimize it. Because resource files account for the largest proportion in APK, we should start with resource slimming. Try to save only one image resource There will be a drawable or mipmap directory under the development directory to adapt to different dpi screens. The following is the dpi range adapted by different named directories Currently, most of the models on the market are within the adaptation range of xxhdpi, so you can consider only retaining one image resource under the xxhdpi directory. The specific resources under which directory and how many resources to retain should be determined according to the actual model distribution of the application itself. Use Drawable XML, Color, .9 PNG instead of PNG
Use JPG instead of PNG Use JPG instead of PNG. Since JPG does not have an alpha channel, the file size is smaller and it can be considered for images that do not require transparency. Be careful when using WebP instead of PNG Since WebP has good effects and WebP files are much smaller than PNG files under the same effects, many people on the Internet say that WebP should be used instead of PNG. I disagree with this. The reasons are as follows:
Lossy audio files instead of lossless audio files From the following official document https://developer.android.com/guide/topics/media/media-formats.html You can see the audio and video formats supported by the Android platform. The following are commonly used lossy and lossless formats (don't think that lossy encoding means poor sound quality):
In actual development, if you need to use audio files, try to use lossy formats such as MP3 and Ogg, and try not to use lossless audio such as WAV and PCM. Remove unused resources The removal of useless resource files here is mainly divided into two parts: not packaging unused resources and deleting unused resources.
You can see all unused resource files. It is recommended to clean up these useless files regularly. On the one hand, it can reduce the size of the project. On the other hand, too many resource files will cause the resources.arsc file to become larger and larger after packaging. The company has a project whose resources.arsc file has reached 2-3 MB, which is a bit surprising. Combining the above points, we can effectively reduce the size of the res folder, assets folder, and resource.arsc file in our installation package, thereby achieving the purpose of weight loss. tool The previous chapter mentioned the optimization ideas. This chapter organizes the tools used in the optimization process.
The above are the tools I used in the optimization process that I think are good. If you have better recommendations, please feel free to add them. In addition, when compressing images, do not compress all the images in the resource directory at once just for the sake of convenience. Many times, the person who worked on this project before may have compressed some resource files, which can easily lead to secondary compression and cause some image distortion. Here I suggest that you go to the resource directory of the application and sort the resource files from large to small, set a standard, such as if you want to compress images over 20KB, then copy these qualified images for compression, and replace the corresponding image resources before optimization after processing to ensure that there is no distortion. The same applies to audio files. Native library slimming Native library slimming mainly reduces the support for CPU architecture. The configuration is very simple. Use abiFilters in build.gradle to configure the required CPU architecture and remove the unnecessary compatible so files from the project. According to the distribution of our users' models, we only retain support for armeabi-v7a. Note that this needs to be decided based on the actual situation of our own products. Since I was not very familiar with the CPU architecture distribution before, I would like to thank Zhang Shaowen from WeChat, Xu Yisheng from Hujiang, and Zheng Xiaobin from Huya for some popular science. In summary, we can effectively reduce the size of the lib folder in our installation package, thereby achieving the purpose of weight loss. Another approach is to generate a separate installation package for each CPU architecture by configuring include in build.gradle. Although it looks good, many domestic application markets do not support this approach of configuring a package for each CPU when listing. Therefore, this approach is relatively useless and is not recommended. If the application is only on Google Play, it is indeed much better than configuring abiFilters. Code Slimming There are many things you can do here, mainly as follows:
To sum up, we can effectively reduce the size of the dex file in our installation package, thereby achieving the purpose of weight loss. Conclusion During the whole optimization process, I optimized the project from 18 MB to 12.5 MB. Some of the above optimization points are affected by other reasons and can only be temporarily abandoned. You can consider including them in the next optimization schedule. These are the routines. When practicing, please make decisions based on your own projects and give priority to optimizing the parts with higher cost performance (cost performance = size that can be optimized/time required). |
<<: Miscellaneous notes on using git
>>: TensorFlow 1.0: Unlocking machine learning on smartphones
UBS’s recent fourth and fifth reports are very in...
Hello everyone, I am Brian. Icons are the most ba...
Author: Xue Qingxin, registered dietitian Reviewe...
In July this year, the official TikTok revealed a...
At about 3:36 pm on December 18, a rollover accid...
As the National Development and Reform Commission ...
As the largest information flow platform at prese...
Let me ask you a question first. Have you ever he...
AFormChange is a free, open source, simple framew...
The advantages of WeChat mini program investment ...
Course Catalog: The 81 practical lessons of Sheng...
On this 315 Consumer Rights Day, many netizens po...
The US Patent and Trademark Office disclosed a new...