The main reason for doing this was that the company’s network was too slow, and the internal test package was distributed to the test partners through dandelion. However, the package uploaded to Dandelion was not distributed by model like appstore, but contained all resource files, and the final size was 106.5m. The company’s network was too slow, which led to the breakdown of the test partners, and they strongly required them to slim down. In the end, the boss gave it to me.
Open to
Let me show you what I can do
Current IPA size: 106.5M
- After the unused icon is deleted: 95.3M
- After removing unused code: 95M
- Delete the useless image after deleting the code again: 89.8m
- After compressed image resources: 89.5m
- Optimization change space: 89.1M
- Delete unused personal information from FLUTTER: 84.6m
- Delete unused article landing page portions of flutter: 84.4m
- After deleting the default background image of the personal information page local: 74.9m
- Use otool to compare __DATA,__objc_classlist (all classes) and __DATA,__objc_classrefs (classes in use), and then filter the difference again after the useless classes are removed: 74.6M
When I finished, I reviewed my practice again. I felt that the order of operation was inappropriate and there were repeated parts.
Order of suggestions
Delete the code
I deleted the useless pictures first, but after deleting the code, a batch of new useless pictures would come out, so I deleted the code first
- Find unused classes from project remote
The FUI tool detects unused import code files
For runtime reasons, and for safety reasons, classes that are found must be searched individually to make sure they are unused before being removed.
Ipa lost 0.3m after deleting more than 300 files
- Find unused classes from ipA binaries
- Change the suffix to.zip after you get the.ipa file
- Unpack the zip.
- Open the.app file in the Payload folder and right-click the package to see the binary file you want to operate on
- Then use otool -o your binary directory > ~/Desktop/all.txt to export the content to all.txt
Open all.txt to see an endless list of classes and methods, etc. It is impossible to manually retrieve the difference between __DATA, __objc_classList (all classes) and __DATA,__objc_classrefs (classes in use). Then thinking about the Internet to find a script to do this, did not find a suitable, only to find a script that should have been able to work but now does not work, open the source code to look, should be due to the current binary file format changed.
It’s easy to figure out why, so I copy it, and I change it, and the address is here, and then I get some unused classes again, and I look it up in the project to make sure it’s unused, and then I delete it.
Nearly 300 files were found this time, and the deletion was still only 0.3m less
Delete the picture
I’m done with the code. I’m ready to delete the images
Detect unused images
Using LSUnusedResources, an open source project, you can find a large number of unused images. To be on the safe side, you should search each one to confirm that they are useless and then delete them.
After deleting, delete all 1x diagrams in assets. xcassets, because they are not needed anymore
Delete picture resource effect is more obvious, direct less 25.9M
The compressed image
The tool used is ImageOptim
The compression effect is super obvious, all the images work, but the IPA is only 0.3m missing after packaging, which is very confusing, we found this paragraph in the headline article later (after reviewing some documentation, we learned that Xcode has a step in the build process called Compile Asset Catalog. In this step, Xcode compresses the PNG image itself and compresses it into a format that can be read quickly. If we compress a project image with ImageOptim, Xcode will use its own algorithm to recompress it during the compile Asset Catalog process. This “recompression” process “rolls back” the ImageOptim compression, which may actually increase the image size. “, and why is it that I still have the effect of 0.3m? Some pictures in the project are not in assets. xcassets.
Modify xcode configuration
- Optional Level–>Fastest,Smallest[-OS]
- Link-time Optimization: a feature of the LLVM compiler that optimizes global code while linking intermediate code. This optimization is done automatically, so there is no need to modify existing code. Apple used Incremental, a new optimization, to significantly reduce link time.
- Deployment Postprocessing, Strip Linked Product, Strip Debug Symbols During Copy, Symbols hidden by If default is set to YES, unnecessary symbol information can be removed to reduce the size of the executable file. However, after removing the symbolic Information, we can only use dSYM for symbolization, so we need to change the Debug Information Format to DWARF with dSYM file.
- Dead Code Stripping(available for static languages only) : Removes unreferenced Code in the statically linked executable. Setting Debug to NO and Release to YES reduces the size of the executable file. This option is enabled by default in Xcode. Static language compilers such as C/C++/Swift remove unused code at link time, but it is not valid for dynamic languages such as Objective-C. Because Objective-C is built on top of the Runtime, all the underlying code that is exposed to the compiler is Runtime source compilation, all of which should be recognized as valid code.
- Generate Debug Symbols(useful but not recommended): When the Generate Debug Symbols option is set to YES, each source file is compiled with -g and -gmodules. Packaging generates the Symbols file. If set to NO, NO symbol file will be generated in ipA and the IPA size can be reduced. But it affects the location of the crash. Keep the default function enabled and do not change it.
- Optimization changed to space
This was tried a lot, but Optimization changed to space with a significant loss of 0.4M
other
- LinkMap can figure out the space occupied by each class or library (code segment + data segment), which is convenient for developers to quickly locate the class or static library that needs optimization (with this I found a very large background map, 2M of 2x, nearly 4M of 3X). The effect is very obvious after deletion
- In addition, you can use LinkMap and otool to find out unused methods. I did not do this, and I have time to modify the script later
To sum up, in fact, most of the lost space is taken up by discarded image resources, so I think the team should develop a good habit, discarded code and image resources should be deleted in time, regular detection and cleaning of garbage files, otherwise over a long period of time, a little bit of each version will eventually become big.