1. 程式人生 > >iOS 瞭解Xcode Bitcode

iOS 瞭解Xcode Bitcode

級別:★☆☆☆☆
標籤:「Xcode Bitcode」「iOS Architecture」「arm64e」
作者: WYW
審校: QiShare團隊

最近專案中接入某第三方SDK後,打包的時候發現有如下報錯:xxx.o was build without full bitcode error :Linker command failed with exit code 1。 然後經過搜尋,設定Enable Bitcode 為 NO,就沒有這個報錯了。筆者簡單瞭解了一下Bitcode,今天給大家介紹一下。

Xcode之Bitcode

  • BitcodeXcode7
    的新特性。
  • 檢視Bitcode:TARGETS -> Build Settings -> 搜尋Enable Bitcode ,示意圖如下:
    Xcode Bitcode

Bitcode的官方說明

官方: Bitcode is an intermediate representation of a compiled program. apps you upload to App Store Connect that contain bitcode will be compiled and linked on the App Store. Including bitcode will allow Apple to re-optimize your app binary in the future without the need to submit a new version of your app to the App Store. For iOS apps, bitcode is the default, but optional. For watchOS and tvOS apps, bitcode is required. If you provide bitcode, all apps and frameworks in the app bundle (all targets in the project) need to include bitcode.

翻譯: Bitcode

是編譯後的程式的中間表現,包含Bitcode並上傳到App Store Connect的Apps會在App Store上編譯和連結。包含Bitcode可以在不提交新版本App的情況下,允許Apple在將來的時候再次優化你的App 二進位制檔案。 對於iOS Apps,Enable bitcode 預設為YES,是可選的(可以改為NO)。對於WatchOS和tvOS,bitcode是強制的。如果你的App支援bitcode,App Bundle(專案中所有的target)中的所有的Apps和frameworks都需要包含Bitcode

看了以上內容,我們就可以對Bitcode有一個簡單的瞭解了。那麼如果我們專案中在使用某些Framework或.a的時候,遇到了類似筆者遇到的錯誤的時候,我們就需要

檢視所用的Framework或.a是否支援bitcode

檢視framework或者.a檔案是否支援bitcode,支援哪些架構:

  • 首先給大家介紹一個工具:otool
  • 說明:
    otoolobject file display tool.
    用於檢視object file的工具。

我們可以使用otool檢視framework或者.a 是否支援設定Enable BitcodeYES,在終端中使用如下命令檢視:

  • otool -l framwork路徑下的實體檔案 | grep __LLVM

說明: 使用otool 工具 檢視framework檔案的load commands內容,然後搜尋load commands中的__LLVM

如果上述命令的輸出結果有__LLVM,那麼就說明,所用的framework.a支援設定Enable bitcodeYES,否則不支援。

  • 示例:
 otool -l /Users/wangyongwang/Documents/QiBitcode/QiBitcode.framework/QiBitcode | grep __LLVM
複製程式碼
  1. 如果上述命令沒有輸出結果,那麼說明所用的framework.a不支援設定Enable bitcodeYES

  2. 如果有如下的輸出結果,那麼說明所用的framework.a支援設定Enable bitcodeYES

   segname __LLVM
   segname __LLVM
   segname __LLVM
   segname __LLVM
複製程式碼
  • App支援Enable Bitcode的必要條件:
  1. 使用的framework或者.a 檔案支援設定 Enable bitcode為YES;
  2. 使用的framework或者.a 檔案支援的架構是齊全的;
  • 那麼為什麼有些framework沒有做成支援Enable bitcode的方式呢?

我查到了如下資料:可能筆者自己理解上還有些問題,筆者就不解讀了,大家自行解讀。

  1. Build static library or framework via Xcode 7, while user build application using Xcode 6. "Framework and library providers need to include bitcode for Xcode 7 development, and Xcode 7 generates bitcode by default. However, bitcode-enabled framework and library products do not work well with Xcode 6. If you still need to support Xcode 6 development, you must produce an additional version of your products without bitcode. To build a library without bitcode, either use Xcode 7 with the build setting Enable Bitcode disabled (ENABLE_BITCODE=NO) or use Xcode 6."
  • 檢視framework支援的架構有哪些:

先給大家介紹下lipo

lipo : Create or operate on a universal file: convert a universal binary to a single architecture file, or vice versa. 建立或者是操作一個通用檔案,轉變通用檔案為單獨的架構檔案或者反過來轉變單獨架構檔案為通用檔案。

給大家介紹一下檢視Framework支援的架構,這裡我們會用到lipo info

lipo info解讀

-info Briefly list the architecture types in the input universal file.
Lists the names of each archive. 簡單地列舉出來輸入的通用檔案的架構型別,列舉出來每個架構的名字:

  • 使用方式:lipo -info framework或者.a實體檔案路徑
  • 使用示例:
 lipo -info /Users/wangyongwang/Documents/QiBitcode/QiBitcode.framework/QiBitcode
複製程式碼

結果示例:Architectures in the fat file:/Users/wangyongwang/Documents/QiBitcode/QiBitcode.framework/QiBitcode are: armv7 i386 x86_64 arm64

關於Architectures:

截止到2018年Apple新發布了iPhone XS, iPhone XS Max, iPhone XR後,iPhone及CPU對應情況:

CPU iPhone
armv6 iPhone, iPhone 3G
armv7 iPhone 3GS, iPhone4(GSM),iPhone 4(CDMA),iPhone 4S
armv7s iPhone 5, iPhone 5C
arm64 iPhone 5S, iPhone SE, iPhone 6, iPhone 6 Plus, iPhone 6s, iPhone 6s Plus, iPhone 7, iPhone 7 Plus, iPhone 8, iPhone 8 Plus, iPhone X
arm64e iPhone XS, iPhone XS Max, iPhone XR

對於iPhone而言:iPhone 5S之前使用的是32位微處理器,iPhone 5S及之後都是64位的微處理器

模擬器上使用的CPU情況由所用的電腦確定

CPU iPhone
i386 32 位微處理器
x86_64 64 位微處理器

關注我們的途徑有:
QiShare(簡書)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公眾號)

推薦文章:
iOS 重繪之drawRect
iOS 編寫高質量Objective-C程式碼(八)
iOS KVC與KVO簡介
iOS 本地化(IB篇)
iOS 本地化(非IB篇)
奇舞週刊