iOS 了解Xcode Bitcode

avatar
奇舞团移动端团队 @奇舞团

级别:★☆☆☆☆
标签:「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篇)
奇舞周刊