The original address
As the business continues to iterate, the APP installation package will get bigger and bigger. Large packages affect user experience, upgrade rate, failure to submit to the App Store, and failure to download in non-wifi environments. How to slim App has become a top priority.
APP Thinning
App stochastic is a technology launched by apple to improve the App download process, and is only available for iOS9 and above. It mainly solves the problem that users consume too much traffic when downloading apps, and also saves the storage space of iOS devices.
There are so many different iOS devices on the market right now, and if you want to look good on multiple screens, you need a lot of optimization resources to match different screen resolutions. The APP also includes optimizations for arm64, ARM7s, ARM7, 3D graphics (OpenGL, Metal, etc.), audio and other different files. If these resources are all in one package, the user’s installation package will inevitably become larger.
App 胣 Framework automatically detects the user’s device type and downloads only what is available for the current device. For example, the iPhone 6 will only download images in 2x resolution, and the iPhone 6 Plus will only download images in 3x resolution. Users only need to download what they need for the specific device they are currently using, which not only speeds up downloads but also saves storage space on the device.
App Thinning type
App Stochastic is comprised of three aspects: App Slicing, On Demand Resources, and Bitcode.
App Slicing
Once an App is uploaded to iTunes Connect, it is sliced and created into different variants, one of which contains only executable architectures and resources for a target device, so that it can be adapted to different devices.
On Demand Resources
On-demand resources are files that can be downloaded after the app is first installed. For example, when you unlock a specific level in a game, you can download content related to a new level. In addition, levels already passed by the player can be removed to save storage space on the device.
You can enable on-demand loading by setting the Build Settings in Xcode.
Bitcode
Bitcode is a bit abstract, but in essence it’s a new way for Apple to optimize apps before users download them. Bitcode makes apps run quickly and efficiently on any device. Bitcode automatically compiles the app using the latest compiler and optimizes it for a particular architecture.
Bitcode doesn’t download app optimizations for different architectures, only device-specific optimizations, making downloads smaller.
This can be done by setting bitcode to YES under the Build Settings option.
How to use App Seismic?
While Xcode and the App Store enable you to perform the main Framework of App Climbing, you need to make sure your App supports this new technology. First, you must enable the asset directory.
Then add the xcassets directory to add the images.
Add image resources according to the template of Asset Catalog. The added images with 2x resolution and 3x resolution will be created into different variations after uploading to App Store to reduce the size of App installation package. The schema files simply follow the default Settings, and the App Store will create different variations for each device, each containing only the schema file that is required for the current device.
Image resource optimization
Image resource optimization is mainly reflected in two aspects: deleting useless images and image resource compression.
Clear useless image resources
The process of removing unwanted images can be summarized as follows:
- Find all image resource file names (.imageset,.jpg,.png,.gif,.pdf file names).
- Look for the file names found in step 1 in all the code files and resource configuration files in the project (files with the suffix.m,.mm,.swift,.xib,.storyboard,.plist).
- Gets all unused image resource names.
- Once the unwanted resources are identified, they can be deleted.
** Note: ** do not delete “image_%d” images by mistake.
If you don’t want to write a new tool yourself, you can use an open source tool. I suggest two open source tools to use: FengNiao and LSUnusedResources. Comparison of this two tools, we can refer to: www.avanderlee.com/optimizatio… This article.
Clear duplicate image resources
Duplicate resources are not named the same but have the same content.
Fdupes is a Linux utility that can find duplicate files in specified directories and subdirectories. Fdupes identifies duplicate content by comparing MD5 signatures of files and comparing files byte by byte.
Image compression
At present, a better compression scheme is to turn the picture into WebP. WebP is an open source project of Google. The principles and benefits of WebP have been covered in the iOS Startup optimization first screen image loading optimization article, which you can refer to if you are interested.
When Google open source WebP, it provides a picture compression tool, CWebP, to convert other pictures into WebP
Note that WebP has a higher CPU consumption and decoding time than PNG. So, we sometimes have to make a trade-off between performance and size.
Therefore, a case-by-case analysis, if the picture is large, you can consider using WebP; When you are younger, you can use TinyPng or ImageOptim for image compression. These two tools do not compress as much as WebP, do not change the image compression method, and do not increase the performance loss when parsing.
Code optimization
Slimming code means finding and deleting useless code. The process of finding useless code is summarized as follows:
- Find all the classes and methods.
- Find the classes and methods used.
- Take the difference between the two to get useless code.
- After manually identifying unwanted code, it can be deleted.
Use AppCode to find useless code
AppCode can check for unused files, spelling problems, type checking, and many other potential problems. Code->Inspect Code for static analysis:
There are some drawbacks to using App Code to find useless Code:
- If a subclass calls a method of its parent class, that method is not considered used.
- Using a property as a dot is considered unused.
- Classes created by string concatenation and methods called cannot be recognized.
LinkMap combines Mach-O to find useless code
LinkMap can be analyzed to obtain information about all classes and methods. LinkMap can be obtained by Setting the Write LinkMap File in Build Setting to Yes, and then specifying the Path to LinkMap File. The Setting options are as follows:
LinkMap contains three parts :Object File, Section and Symbols.
Object File: contains all files of the code project; Section: describes the offset position and size of the code segment in the generated Mach-O; Symbols: Lists each method, class, block, and size;
Through LinkMap, not only can all methods and classes be counted out, but also the specific package size occupied by the code can be clearly seen, so that code optimization can be targeted.
Once you have the complete set of code information, you also need to find the classes and methods used to get the difference set, so as to find the useless code. The Mach-O file contains all the classes and methods used.
All methods in iOS are called through objc_msgSend, and objc_msgSend gets the selector parameter from the __objc_selrefs section in Mach-O, So the method in __objc_selrefs must be called. __objc_classrefs is the class that was called, and __objc_superrefs is the class that called super. With __objc_classrefs and __objc_superrefs, we can find out which classes and subclasses are used.
You can use the MachOView software to view information in the Mach-O file.
Check us out for more iOS skills