“This is the 21st day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
Package slimming scheme
I believe that we all encounter that with the change of project requirements, the code and resource files in the project will be accompanied by growth, most of the time, we will not easily delete some code, or resource files. But this comes at the cost of getting bigger and bigger bags.
When the size of a package exceeds 200MB, it is noticed by the data department, which analyzes that the decrease in new users may be due to the application store package size exceeding the limit and users’ traffic not being able to download. Doing package size optimizations at this point would mean losing a lot of new users and driving up the cost of new users. So package size optimization should not be done after the problem occurs, it should be done immediately, and we should also consider 200MB as the “red line”.
Let’s look at some options for package size optimization:
The first is resource file optimization, which is easiest to see on thin packages. If you need to slim down your package size, try this first. You can start with Image framework compression, garbage checking, upload, or use the official App Framework.
Secondly, it is code file optimization, code file optimization investment time may be much longer than resource file optimization, and the effect is not so obvious. But code file optimization can check a lot of useless code, and delete a lot of unnecessary files, clean up the project structure, make the project sustainable maintenance.
Resource file optimization
Comparatively speaking, the time of resource optimization is the fastest, and the results are the best. We only need to do the usual resource compression processing, regularly clean up useless resources, and do a good job in the reasonable allocation of resource files, so the package size will not be too large.
Image compression
Image compression we can choose from a lot of tools in fact, the key is to pay attention to check the compressed image resources will not have a problem in the display. Recommended tools: ImageOptim, image pressure.
Image resource compression also has many companies using webP format pictures, this format is much smaller than PNG, JPG, but the client performance will have a certain impact on the decoding of WebP pictures. On some older models, it didn’t work so well. So there are trade-offs. There is isparta, a tool for WebP and PNG conversion
In fact, I think these two schemes can coexist. Set a maximum resource file size within the team. For example, if the resource file size exceeds 100KB, use webP format, and if the resource file size is less than 100KB, use compression tools. This can greatly reduce the size of the resource file, as well as the size of the package.
Useless Resource Check
Useless resource checking can be done with the tool LSUnusedResources.
The process of deleting unnecessary images can be summarized as the following steps.
- through
find
Command to get all the resource files in the App installation package, such asfind /Users/daiming/Project/ -name
. - Set the type of resource to be used, for example
JPG, GIF, PNG, webp
. - Use regular matching to find the name of the resource used in the source code, such as pattern = @”@”(.+?). “”.
- use
find
Command to find all the resource files, and then remove the resource files used in the code, the remaining are useless resources. - For rule-based resource names, we need to add rules to the regular expressions that match the resources used, such as @ “image_%d”.
- Once the unwanted resources are identified, they can be deleted. This delete operation, you can use
NSFileManger
The system class provides functionality to do this.
Note that you need to check whether the suffix of some resource files is concatenated, such as “home_btn_normal”, “home_btn_select” or “bg_1”. “BG_2” is checked by adding the corresponding regular expression in step 5.
Uploading resource files
If there are still a lot of resource files involved, we can consider reasonably uploading some resource files to the server, and some servers will deliver them to the mobile terminal. This kind of scheme usually requires multiple parties to formulate the scheme uniformly so as to achieve maximum utilization.
App Thinning
Apple launched App Stochastic in order to address the issue of high-throughput App download, while also saving storage space on users’ iOS devices.
App Stochastic selects present-only content for download, tailored to different devices. For example, the iPhone 8 will only download images in 2x resolution and the iPhone 8 Plus will only download images in 3x resolution.
App Stochastic comes in three ways, including App Slicing, Bitcode, on-demand Resources.
-
App Slicing — chopping up your App after you upload it to iTunes Connect, creating variations that work on different devices.
-
Bitcode is used to optimize packet size for a specific device, and the optimization is not obvious.
-
On-demand Resources, primarily for multi-level scenarios in games. It will download resources for subsequent levels according to the user’s level progress, and the resources that have passed the level will also be deleted, thus reducing the package size of the initial App.
How can you use App Stochastic in your project? Most of the work is done by Xcode and the App Store. Just add xcassets to Xcode and add images.
Code file optimization
Code file optimization can also be viewed as an optimization of the executable file Mach-O, whose size is determined by the amount of code. So slimming down Mach-O is all about finding and eliminating useless code.
Useless code checking
useAppCode
Analysis of the
Useless code can also be checked with a simple tool, such as AppCode, of course, if there is too much code, this may not be able to analyze. It is said that millions of lines of code may be overwhelmed.
AppCode analysis method is very simple, directly in AppCode select Code -> Inspect Code can be static analysis. After static analysis, we can see all the Unused code in the Unused code.
While a lot of useless code can be detected, it is not accurate and requires a second manual verification to safely remove it.
Analysis of theMach-O
file
Another way to analyze mach-O files is to use MachOView to view the information in the Mach-O files. Then the mach-O file’s __objc_selrefs, __objc_classrefs, and __objc_superrefs are used to see the classes and subclasses used.
This approach is not perfect because objective-C is a dynamic language and method calls can be written as dynamic calls at run time, which makes it impossible to collect all the methods and classes that are called. Therefore, the useless methods and classes we find in this way are only for reference and need to be double-checked.
Reducing third-party SDKS
If you are using a lot of third-party libraries in your project, you also need to check that you have imported libraries with the same functionality to avoid overloading resources by importing too many libraries.
Sometimes when a third party library has a high size ratio, you need to consider whether to replace it.