APK slimming notes for up to 53% Compression
1. How do I think about this?
APK is the file format of the Android installation package. This topic is a common topic. Many methods and rules have been summarized by our predecessors, including internal and external networks. However, with the rapid development of mobile technology over the past two years, some new ways of thinking and optimization methods have gradually emerged and matured. In practice, I have learned some experiences and made some thoughts and conclusions here. I hope to give you some help and reference when you are engaged in related work, we also hope that you will discuss this open topic together.
This is not to mention why the APK should be reduced. It only takes three aspects for you (or the customer). The larger the APK, the larger the APK will be during the download and installation process, the more traffic they consume, the longer the installation wait time. For the product itself, the lower the download conversion rate (because of competing products, users have more opportunities to choose the one with the best experience, the most functions, the best performance, and the smallest package). for R & D, it is an opportunity to optimize and improve the technology.
To slim down, we should first look for the reasons and problems of fatness. The goal-path-resource-based thinking model provides the following paths for finding the cause and problem. One is to shoot your head, based on your own experience and judgment, or even subjective imagination; the second is to go to the search engine to find keywords, visit various technical forums to hear what technical experts say, read various technical articles for extraction and extraction, and the third is to use a measurable tool or method to find problems.
I will not repeat the first two methods here. An measurable tool or method is used for analysis. To do this, you must first sharpen your tools. This tool can be forged by itself or ready-made. We recommend an online apk analysis tool. Because it is an external tool, please do not upload unpublished products during use. For data security, the author uses an open-source Android project on github as an example of slimming.
2. Search for Problems
NimbleDroid is a system developed by the doctoral team at Columbia University to analyze the performance indicators of Android apps. The analysis methods are static and dynamic, among them, static analysis can analyze the ranking of large files in the APK installation package, the size of various well-known sdks and the proportion to the Code as a whole, the size and ranking of various types of files, the number of methods in various well-known sdks and the proportion of the methods in all dex are not much nonsense. Let's look at the face value in the big picture without a code in HD below.
If you want to use the analysis function to analyze your product, log on to and upload the apk package of your product. All functions are currently free of charge. If you want to analyze the products that have been released on Google Play, you can directly click "Play Apps" to view the results. You can also use the search function to view the results based on the Application name and package name. Again, do not upload any unpublished products.
Login
VcmlnaW5hbD0 = "http://www.bkjia.com/uploads/allimg/160402/042119A30-1.png" src = "http://www.bkjia.com/uploads/allimg/160402/04211955K-0.gif"/>
Upload an apk File
The analysis result summary shows some overview information, apk file size, and total number of methods.
File Size Analysis details page, large file list, which lists the ranking of over K files in the apk file. The file size here refers to the size in the apk file.
The size of various well-known sdks and the total proportion of codes can be identified here: Android Support, Jackson JSON parser, Google Play Services, Paypal, Glide, OkHttp, Facebook SDK, Fabric, gson and so on. Application indicates the code written in the App.
Size and ranking of various types of files
Percentage of well-known sdks in all dex Methods
Ranking of methods of various well-known sdks
Is there a cool feeling after reading the section chart in this apk! I compared this analysis tool to the smart weight scale we bought at home, which can be called weight, fat content, bone weight, bone density, muscle content, etc. So did we find some problems, then, we can use logic to link these problems with our previous experience and head shoot.
Then, we can sort out our optimization objectives through analysis data.
1. In the list of large files, 11 png files exceed 100 kb. Remember, this is compress;
2. In the ranking of large files, the size of resources. arsc is close to 2 MB, which is also an optimization point;
3. In the ranking of large files, classes. dex is close to 3 MB, and classes. dex is the carrier of the Code. The optimization of this part needs to be subdivided. Then let's look at the ranking of SDK segments;
4. component proportion, Android Support, Jackson JSON Parser, and Google Play Services are the top three libraries;
5. In the file type ranking list, png, dex, and arsc are the top three;
3. Sort out the optimization objectives
3.1 image optimization attempts
The first is the first goal, image optimization, and slow down. Let's take a look at why these images are so advanced. To be precise, why are these images so large in apk (actually zip files, now, go to the tool for analysis.
This time I used some simple tool combinations, and the built-in cmd is good.
The command execution result is as follows:
Well, all the png files are stored in the apk using the STORE method. For details about the STORE and DEFLATE in the zip file, see)
In layman's terms, when a file is STORED as a zip file in STORED mode, it indicates that the file has not been compressed. If it is in Defl: N mode, DEFLATED normal compression to zip.
This seems a bit unreasonable. If the png is unblocked and put in the zip file, the final output apk will be relatively large. So how can we solve it? I first tried android gradle plugin and found that both aaptOptions and packagingOptions could not solve the problem. I found an open-source project AndResGuard on github and tried to integrate it into the project. The result is as follows:
Before optimization:
10536027 bytes
After optimization:
Common zip compression: 8786265 bytes (compressed by nearly 17%)
7 zip compression: 8567150 bytes (compressed by nearly 19%)
Let's take a look at what this tool has done.
Before Optimization
After Optimization
1. obfuscation of resource (png, xml, jpg, etc.) names, confusion of resource path names, and compression of name length;
2. The png files originally STORED in the zip file in STORED form were changed to the DEFLATED (Common compression storage) method;
3. unexpected discovery of resources. arsc, META-INF/*. SF and META-INF/*. MF are smaller, and the size of the files is also smaller after decompression.
Use the apk decompilation tool jadx javasapk to find the truth
The relative paths of resources (png, xml, and properties files) in the original apk will be stored in the META-INF /*. SF and META-INF /*. MF and calculate the SHA1 value for each resource file and store it in these two files, for the differences and functions between the two sha1. please refer to the articles on this knowledge on the Internet, which is beyond the topic of this article, so I will not go into details here.
For resources. arsc files
It is easy to see that it is a resource file index table. Therefore, you should understand why these three files have become smaller.
3.2 unexpected discoveries
Look down at resources. arsc and find something interesting,
This will become an optimization point, removing useless translation resources and introducing some third-party sdks. These sdks often include a lot of translation resources, such as the android support library, let's take a look at the effect.
Suppose we only keep the English language, but of course it's just an experiment. In reality, let's look at the specific situation,
Use 7zip compression: 8220738 bytes (compressed by nearly 22%, followed by three more points)
Of course, this is not possible in real projects, but mosquito meat is also meat!
In fact, what I want to talk about is that this provides an optimization idea, that is, to use gradle configuration to eliminate useless resources. It can also be used on the so local library, resolution (the gradle configuration is deprecated.
Gradle configuration example:
Remember that the package is in the middle of android. So someone asked, is there no x86 in abi? It is said that intel provides a solution named houdini, which is a middleware running on x86 devices. It can transcode arm into x86 commands, but the efficiency is very low, for example, the calculation of MD5 and SHA1 is not even as good as that of java. I have tested and compared it with another topic. I will not repeat it here. Interested readers can move on to it.
So far, we have moved towards the first goal and inadvertently discovered the relationship between the first and second goals. Therefore, we achieved the second goal using resource obfuscation tools.
Using 7zip compression, we performed two point compression on the entire package, which is beyond the expectation.
3.3 image Optimization Methods
For the first goal, our path is not over yet. The path produced by shoot our head is to compress png and convert non-alpha images to jpg. What else? So I went to various technical forums and consulted various technical experts. The sorting path is as follows:
1. Perform a manual lint check to manually delete resources that are not referenced in the Code. The actual effect varies.
Open "Analyze" in Android Studio and select "Inspect Code ...", Select the entire project and click "OK"
Configuration example
2. Enable shrinkResources in the gradle script
The script is as follows:
ShrinkResources works better with minifyEnabled. For details, see shrinkResources usage and Note.
Use 7zip compression: 8115283 bytes (compressed by nearly 23%, and then increased by 1 point)
3. use an image compression tool to compress the size of a png Image and convert a non-alpha image to a jpg image. This colleague and the experts on the network have already done a lot of work, for more information, see the appendix.
When using tinypng, I just want to say that we are working on products in the company. This solution is used with caution. Uploading any content of unpublished products to the external network may cause data leakage. Therefore, we should use this solution with caution. The following describes the alternative solution.
WASTED
Pngquant
ImageAlpha
ImageOptim
The above tools are too scattered and there are no integrated tools. The answer is "yes". @ imagemin developed by xinlun kids shoes
@ MSImageResourcesHelper
Converting png to jpg has different effects.
4. Convert png to webp as the ultimate killer. For more information about webp, see Google official documentation and Android developer online reference.
First:
7 zip compression: 4926912 bytes (compressed by Nearly 53%, and then increased by 30 points)
Right, it's 30 o'clock. Currently, the apk size is less than half of the size of the original apk, but I have changed the code line and only used some tools!
Speaking of things, I have to take diet pills, and I have a hunger strike, but I lose half of my weight !!!
However, the project is currently unavailable because there are two pitfalls.
On Some Samsung models, some images with an alpha background have a clear black line, which won't happen here, currently, this problem is not solved by creating a webp graph through a whitelist;
4 In Xiaomi 2. xx's mobile phone fails to correctly identify the webp image described in the xml file, causing loading of the xml layout file after the interface is up. The webp file fails to be loaded, and the error message "resource file not found" is returned, this causes the app to crash. The tracking found that the class Resource of Xiaomi machine is MIUIResource, but the MIUIResource does not correctly identify webp, which causes the loading of the Resource file to fail. It is preliminarily determined that there is no solution currently, therefore, we can only give up this optimization solution.
Here is the optimization of image resources for the first goal.
3.4 code optimization
The second goal has been achieved, and the third goal is the code optimization. Sort out the following optimization paths:
1. Enable proguard code optimization
Set proguardFiles to getdefaultproguardfile('proguard-android.txt'folder, 'proguard-project.txt'
Change to proguardFiles getdefadefaproguardfile('proguard-android-optimize.txt'folder, 'proguard-project.txt'
For more information about enabling code optimization, see the appendix.
2. Remove useless Libraries
If the minimum version supported by apk is API14, and the Code does not use APIs higher than api14, you can consider removing the entire android support library.
3. Use a smaller database instead
If only google statistics are used, do not integrate the entire google play services.
4. Clear obsolete code regularly
Regularly Delete useless logic and expired business function modules, as well as discarded A/B test code.
5. The business module uses a plug-in framework to dynamically pull code from the cloud
Plug-ins. This is another issue. I will not repeat it here.
The final result of apk slimming
10536027 bytes compressed to 4926912 bytes, compressed by Nearly 53%
Summary
1. enable resource obfuscation and resource compression in the script
2. Use 7zip instead of zip
3. Enable code obfuscation optimization and useless resource deletion in the gradle script
4. Use a smaller image and a compression tool to compress the image size.
5. Remove useless resources, languages, local so libraries, third-party libraries and resolutions
6. Use a smaller database
7. Try to completely kick the android support library out of your project
8. regularly clear code
9. Try to write the interface in H5 and get the image from the cloud
10. Try to plug-in the business module
11. Find the files in the zip folder that are stored in the STORE format (not limited to the raw directory), try to compress the files, and load these resources in an alternative solution.
12. Try webp's image loading solution and seek breakthroughs
Finally, continue learning and try new optimization solutions.
In this document, we will give the technology "slimming and product failures!
Appendix
How to Reduce the apk size by 6 MB
Android APP ultimate slimming Guide
APK slimming practices