iOS App Thinning 实践

2,431 阅读4分钟

最近接手到app优化的任务,基于前人的文章以及相关资料完成本次任务的同时对整个过程做一个简单的记录,希望对看到的朋友有帮助,也欢迎指出文章中出错的地方,可以随意喷。第一次发文,多多包涵。

ipa安装包组成

  • Symbols
  • Payload
    • nib
    • app二进制文件
    • bundle
    • Assets.car
    • 其他文件

资源优化

1、使用Imageoptim压缩图片

使用Imageoptim需要在xcdoe设置compress png files 编译选项为NO,二者不能同时使用。详见Imageoptim官网说明

2、使用工具清理未使用的图片

3、 删除其他无用资源文件

  • 音视频文件
  • db、plist、json、readme 等无用文件

编译选项优化

Optimization Level

该选项表示编译器优化代码性能的不同方式。具体表现为项目大小或者编译时间。实现机制为不同程度的减少代码执行需要的loop unrolling(一种牺牲程序的尺寸来加快程序执行速度的优化方法)。优化程度从fast,faster,fastest依次增强。这些选项会给编译器权限去修改代码的结构以便节省编译时间。任何形式的代码优化都会以时间为代价

Release模式设置为Fastest, Smallest [-Os]可以显著的减小编译文件大小,该选项会自动尝试fastest和smallest两种极端优化方法,选择最合适的值。苹果建议只为Release模式设置该选项

Optimization Leveal各个选项的意义

Xcode Setting Describtion
None The compiler does not attempt to optimize code. Use this option during development when you are focused on solving logic errors and need a fast compile time. Do not use this option for shipping your executable.(编译器不进行任何代码优化)
Fast The compiler performs simple optimizations to boost code performance while minimizing the impact to compile time. This option also uses more memory during compilation(编译器进行小幅度代码优化,同时消耗更多的内存)
Faster The compiler performs nearly all supported optimizations that do not require a space-time tradeoff. The compiler does not perform loop unrolling or function inlining with this option. This option increases both compilation time and the performance of generated code.(该选项会进行所有可用的优化选项而不用花费额外的时间和内存。该选项不会执行循环展开或者内嵌函数。该选项会在提升代码性能的同时增加编译时间)
Fastest The compiler performs all optimizations in an attempt to improve the speed of the generated code. This option can increase the size of generated code as the compiler performs aggressive inlining of functions.This option is generally not recommended.(该选项会作出尽可能多的尝试来提高编译性能。但同时会和内嵌函数机制存在冲突。一般不建议使用该选项)
Fastest,Smallest The compiler performs all optimizations that do not typically increase code size. This is the preferred option for shipping code because it gives your executable a smaller memory footprint.(编译器会进行所有可用的优化而不显著的增加运行空间。该选项是打包代码的最优选项)

Enable bitcode

设置为YES(项目中所有的库都必须支持bitcode)

Bitcode为LLVM编译器的中间代码的一种编码,LLVM的编译原理是前端负责把项目代码翻译成Bitcode中间码,然后再根据不同目标机器芯片平台转换为相应的汇编指令以及翻译机器码。使用Bitcode的不便之处是无法还原崩溃现场找到原因。

Strip Symbol Information

1、Deployment Postprocessing
2、Strip Linked Product
3、Strip Debug Symbols During Copy
4、Symbols hidden by default

设置为YES可以去掉不必要的符号信息,可以减少可执行文件大小。

Release 设置为YES Debug 设置为NO,

Dead Code Stripping

删除静态链接的可执行文件中未引用的代码

Debug 设置为NO Release 设置为YES 可减少可执行文件大小

Assert Catalog Compiler

optimization 选项设置为 space 可以减少包大小

Generate Debug Symbols

Enables or disables generation of debug symbols. When debug symbols are enabled, the level of detail can be controlled by the build ‘Level of Debug Symbols’ setting.

调试符号是在编译时生成的。当Generate Debug Symbols选项设置为YES时,打包会生成symbols 文件。设置为NO则ipa中不会生成symbol文件,可以减少ipa 大小

xcode 默认选择为YES,不建议关闭该选项,可能会影响到app调试。

可执行文件优化

1、 使用Appcode(code->inspect code) 代码检测分析功能删除无用类和无用方法。

2、清理掉功能相似以及非必要的的第三方库

总结

xcode在编译层面很多选项都为我们默认开启。整个优化过程中对包大小影响最主要部分还是在于图片资源和无用类无用方法的清理。所以还是要在开发过程中注意图片资源的使用,当某些代码和类没用的时候及时清理,尽可能的避免冗余业务类和方法保持代码的干净整洁。

Reference

Xcode缩小ipa包大小及symbols设置等

App Thinning (iOS.tvOS,wtchOS)

Build Setting Reference)

Tuning for Performance and Responsiveness