How to reduce the size of the ipa package and reduce the size of the ipa package

Source: Internet
Author: User

How to reduce the size of the ipa package and reduce the size of the ipa package

Author: Jin Chao@For more information about Alibaba mobile security, visitAlibaba Cloud universal security blog

Reducing the size of the ios Executable File package is a problem that every ios developer has to go through. Generally, it first processes the resource file and compresses the image/audio, remove unnecessary resources such as @ 2x and @ 3x image merging. After optimizing these resources, we can also try to slim down the executable files. The larger the project, the larger the size of the executable files, because the AppStore encrypts the executable files, this results in a low compression rate for executable files. After compression, the size of the executable files in the entire APP installation package is approximately 80% ~ 90%, it is worth optimizing. Here are some compression points:

1> Compiler Optimization-some configuration items in setting

Build Settings-> Optimization Level has several compilation Optimization Options. for release, Fastest and Smalllest should be selected. This option will enable all Optimization without increasing the code size, make the executable files as small as possible. Of course, these compilation items are configured for the project by xcode by default when the project is generated. If xcode changes, we need to know these items.

2> remove Symbol Information

Strip Linked Product/Deployment Postprocessing/Symbols Hidden by Default should be set to yes in the release version to remove unnecessary debugging Symbols. Symbols Hidden by Default defines all Symbols as "private extern ". These options are currently the default options for release in XCode, but the project generated by the old version of XCode may not be. check whether these options are available.

3> most of the referenced Libraries

A. Some library references may only use a small feature. Some may be used to facilitate one or two cases. For such a library, we can write code by ourselves, or do not need glass functions (we suggest you implement it by yourself, which can save some database references ).

B. For some libraries, the ARC and MRC also have an impact on the size of the database. Because currently, the arc manages the memory, many databases are updated to the arc mode, since the system inserts release in arc mode, it also has an impact on the size. See:

The result is that ARC will increase the size of the code segment by 10%. Considering that the code segment accounts for about 80% of the executable files, it is estimated that the impact on the entire executable file will be 8%.

It can be evaluated whether it is worthwhile to change some modules of the project to MRC for a 8% reduction in volume, so that the maintenance cost of the program has increased. We generally do not recommend this in special cases.

4> useless code

Create a class in the project and add several methods to it, but do not import it anywhere. After building the project, observe the linkmap. You will find that the class is still compiled into the executable file.

Based on the C ++ experience, the class and method compilers that are not used will be optimized and will not be compiled into the final executable file, but the object-c is different, because of the dynamic characteristics of object-c, it can obtain this class and method through class and method name reflection for calling, so even if a class is not used in the code, the compiler cannot guarantee that this class will not be called through reflection at runtime, so as long as it is a file in the project, whether or not it is used or not, it will be compiled into an executable file.

In this regard, we can use scripts to traverse the entire project file and find all class files that are not referenced and methods that are not called, remove them when there are no other dynamic calls. If the entire project lasts a long time and the Code remains a lot left behind, This cleanup will save a considerable amount of space for executable files.

5> class/Method Name Length

By observing linkmap, we can find that each class and method name has a corresponding string value in the _ cstring segment, therefore, the length of the class and method names also affects the size of executable files. The reason is the dynamic characteristics of object-c, because you need to find this class/method for calling through class/method name reflection, the object-c object model will save the class/method name strings.

To solve this problem, we can consider obfuscation of all classes and method names before compilation. Like compressing js, we can replace long names with short names. In addition to reducing the size, security is also greatly improved. The results of the class-dump output from executable files obtained by others are obfuscated class and method names, you cannot guess what a method is doing from the class and method names, so it is difficult to hook the sub-Program for hack. However, there is a disadvantage in this process, that is, the stack method name obtained from the Reverse Solution of the crash stack will be obfuscated. You need to add a new layer of obfuscation-> original name conversion, which means the implementation and use costs are a bit high. In fact, the length of this part is relatively small, and the medium-sized project is several hundred kb. You can try it if you have high security requirements.

6> redundant strings

All static strings defined in the Code are recorded in the _ cstring segment of the executable file. If there are many logs in the project, this space is also considerable, it also has a size of several hundred kb. You can consider clearing all redundant strings. In addition, if there is a particularly long string, it is recommended to extract and save it as a static file, because AppStore encrypts executable files, resulting in a low compression rate, the compression ratio of a particularly long string after being extracted from a static resource file is much higher than that of an executable file.

7> image processing:

We usually compress images or use webp format to process images. In fact, code can be used for most solid color images, which can reduce a lot of resources.

Some people may ask the project's website resources are in a big picture, not a small figure. What should we do? Split: Split the big image into multiple graphs. You can use the code to reduce the code size. Of course, try to reduce unnecessary resources and put them in the resource. Download is not commonly used. Finally, we listed the methods to reduce the size of the iOS installation package as a CheckList chart:

 

 

Author: Jin Chao@For more information about Alibaba mobile security, visitAlibaba Cloud universal security blog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.