iOS App Thinning 實踐


App隨著版本迭代、業務擴張、人員更迭、架構變更使項目變得越來越臃腫。廢棄的業務代碼、冗余無用的類方法和聲明、無用的資源文件穿插在項目中使App的包越來越大。為了保持項目整潔,提升開發和用戶體驗App開啟本次Apps瘦身之旅。


ipa文件構成



通過對包內容構成的分析,可以圍繞資源文件、可執行文件、xcode編譯過程三個方面去做優化。

優化點

資源優化

1、通過工具壓縮圖片清理無用圖片

Imageoptim:圖片壓縮工具,跟xcode編譯設置 compress png files 不能同時啟用。詳見Imageoptim官網說明。
LSUnusedResources、FengNiao 清理無用圖片。

2、壓縮音視頻文件等可壓縮大文件

3、手動清理無效未使用資源(json、text、plist 等文本資源)。

可執行文件優化

1、使用Appcode(code->inspect code) 代碼檢測分析功能刪除無用類和無用方法、清理無效聲明、#import/#include、清理未使用變量。

同時通過Appcode 還能分析出其他項目代碼中存在的問題,也可以一并處理,可避免一些潛在crash,內存泄漏等問題。

2、第三方庫

整合刪除功能相似相近的第三方庫,不要為了一個小功能而引入整個庫。對第三方庫的使用進行嚴格的準入評估,避免濫用。


xcode編譯優化

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 Postprocessing2、Strip Linked Product3、Strip Debug Symbols During Copy4、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調試。

Reference

Xcode縮小ipa包大小及symbols設置等
App Thinning (iOS.tvOS,wtchOS)
Build Setting Reference
Tuning for Performance and Responsiveness

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容