1. 程式人生 > >iOS中Bitcode的介紹及配置

iOS中Bitcode的介紹及配置

今天在一個iOS培訓網站上看到一篇關於第三方庫不包含bitcode就會報錯的文章,感覺剖析得很詳細,分享出來,希望可以對iOS初入門者有所幫助。下面我們就一起來看看吧。

用Xcode 7 beta 3在真機(iOS 8.3)上執行一下工程,結果發現工程編譯不過。看了下問題,報的是以下錯誤:

ld: ‘/Users/**/Framework/SDKs/PolymerPay/Library/mobStat/lib**SDK.a(**ForSDK.o)’does not contain bitcode. You must rebuild it with bitcode enabled (Xcodesetting ENABLE_BITCODE), obtain an updated library from the vendor, or disablebitcode for this target. for architecture arm64

得到的資訊是引入的一個第三方庫不包含bitcode。

Bitcode是個什麼鬼?

查閱了一下官方文件,在App

Distribution Guide–App Thinning (iOS, watchOS)一節中,找到了下面這樣一個定義:

Bitcode is an intermediate representationof a compiled program. Apps you upload to iTunes Connect that contain bitcodewill be compiled and linked on the App Store. Including bitcode will allowApple to re-optimize your app binary in the future without the need to submit anew version of your app to the store.

說的是bitcode是被編譯程式的一種中間形式的程式碼。包含bitcode配置的程式將會在App store上被編譯和連結。bitcode允許蘋果在後期重新優化程式的二進位制檔案,而不需要重新提交一個新的版本到App store上。

而在What’s New in Xcode-New Features in Xcode 7中,還有一段如下的描述:

Bitcode. When you archive for submission tothe App Store, Xcode will compile your app into an intermediate representation.The App Store will then compile the bitcode down into the 64 or 32 bitexecutables as necessary.

當提交程式到App store上時,Xcode會將程式編譯為一箇中間表現形式(bitcode)。然後App store會再將這個botcode編譯為可執行的64位或32位程式。

再看看這兩段描述,都是放在App Thinning(App瘦身)一節中,可以看出其與包的優化有關了。

Bitcode配置

在上面的錯誤提示中,提到了如何處理我們遇到的問題:

You must rebuild it with bitcode enabled(Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, ordisable bitcode for this target. for architecture arm64

要麼讓第三方庫支援,要麼關閉target的bitcode選項。

實際上,在Xcode 7中,我們新建一個iOS程式時,bitcode選項預設是設定為YES的。我們可以在”Build Settings”->”Enable Bitcode”選項中看到這個設定。不過,我們現在需要考慮的是三個平臺:iOS,Mac OS,watchOS。

對於iOS,bitcode是可選的;對於watchOS,bitcode是必須的;而Mac OS是不支援bitcode。

如果我們開啟了bitcode,在提交包時,下面這個介面也會有個bitcode選項:
這裡寫圖片描述

所以,如果我們的工程需要支援bitcode,則必要要求所有引入的第三方庫都支援bitcode。

通過本文對bitcode的概念及配置情況的簡要介紹,希望iOS開發人員在工程執行中遇到類似的情況,可以根據上文的介紹更有效的找到原因並及時處理。