App启动优化之减少动态库

380 阅读1分钟

App启动优化之减少动态库

原理

app 启动的时候需要加载动态库,加载的动态库越多启动越慢,因此我们可以通过减少动态库数量,提高启动速度。这就需要我们将多个动态库合并成一个动态库

测量启动时间

如果连程序启动的时间都不知道怎么确定优化的效果呢?一下有两种方式测量启动时间, 注意需要使用真机

  1. 我们可以设置DYLD_PRINT_STATISTICS 环境变量为 1 或者 YES
  2. 使用instument , APP Launch 模块 测量启动时间

如何将多个动态库合并成一个

将动态库编译成静态库

将动态库Mach-O 类型改为静态库 ,然后进行编译。 最终会产生一个和动态库同样结构的framework,只不过它的执行文件类型变了

/Build/Products/Debug-iphonesimulator/MobileFoudation.framework/MobileFoudation: current ar archive random library

对比一下动态库

Build/Products/Debug-iphonesimulator/MergeFramework.framework/MergeFramework: Mach-O 64-bit dynamically linked shared library x86_64

注意 这里的静态库 不是通过new target -> static library 创建出来这个静态库的目录结构跟动态库是一样的

ls /Build/Products/Debug-iphonesimulator/MobileFoudation.framework
Headers         Info.plist      MobileFoudation Modules         _CodeSignature

集成和使用

  1. new target for dynamically library . (e.g MergeFramework)
  2. link 你所需要的动态库
  3. 在你app中link and copy MergeFramework
  4. 正常使用
import MobileFoudation
import Chart
import FHQRCodeKit

ChartRoot().log()

详情见demo: gitee.com/510491354/m…