如何讓Framework支持Bitcode

讓 Framework 支持 Bitcode的問題:

將工程中的幾個基礎(chǔ)庫打包成動態(tài)庫,減少二進制包大小。在本機build時沒出現(xiàn)啥問題。但是在jenkins上打包,卻出現(xiàn)了如下錯誤:

Error Domain=IDEFoundationErrorDomain Code=1 "Failed to verify bitcode in FLAnimatedImage.framework/FLAnimatedImage:
error: Bundle only contains bitcode-marker

這主要是跟bitcode相關(guān)。因為這些庫默認是支持bitcode的,在build時,compile的參數(shù)是 -fembed-bitcode-marker,它的意思只是標記二進制文件中有bitcode,但是實際上沒有內(nèi)容,一般用于測試。

而我們在jenkins上打包時,先archive,然后再導(dǎo)出ipa包,而archive的編譯參數(shù)是-fembed-bitcode,所以與本地調(diào)試不太一樣。

這兩個參數(shù)有什么區(qū)別呢?

? -fembed-bitcode-marker simply marks where the bitcode would be in the binary after an archive build.
? -fembed-bitcode actually does the full bitcode generation and embedding, so this is what you need to use for building static libraries.
? Xcode itself builds with -fembed-bitcode-marker for regular builds (like deploy to simulator)
? Xcode only builds with -fembed-bitcode for archive builds / production builds (as this is only needed for Apple).

-fembed-bitcode-marker會生成一個最小bitcode的section,但是沒有任何內(nèi)容,size=1。

-fembed-bitcode則會生成bitcode的相關(guān)內(nèi)容。

You should be aware that a normal build with the -fembed-bitcode-marker option will produce minimal size embedded bitcode sections without any real content. This is done as a way of testing the bitcode-related aspects of your build without slowing down the build process. The actual bitcode content is included when you do an Archive build.

所以我們在打包lib的時候,需要設(shè)置參數(shù)-fembed-bitcode,這樣才會包含bitcode的內(nèi)容。

因此,解決方案有兩種:

  • 禁用bitcode

  • 設(shè)置參數(shù)-fembed-bitcode

這里介紹設(shè)置-fembed-bitcode

1、由于我是基于pod package來打包,所以需要需改podspec文件,添加xcconfig即可。

s.xcconfig = {'BITCODE_GENERATION_MODE' => 'bitcode'}

2、如果你是通過xcodebuild來打包lib的話,可以在build setting的User-Define Setting中添加,這樣在build的時候也會是-fembed-bitcode。

'BITCODE_GENERATION_MODE' => 'bitcode'

如下圖:


image.png
如果想讓參數(shù)是-fembed-bitcode-marker,設(shè)置為marker

'BITCODE_GENERATION_MODE' => 'marker

有一張圖說的很清楚。
image.png

另外還要 Enable Bitcode 設(shè)置為 YES 之外。

3、Build Settings -> Enable Bitcode 被設(shè)置為 YES
image.png

還有一種方式,給 Other C Flags 添加編譯選項 -fembed-bitcode。

New 2、給 Other C Flags 添加編譯選項 -fembed-bitcode :
image.png

直接在Other C flags里設(shè)置,可能會產(chǎn)生warning。
推薦使用設(shè)置BITCODE_GENERATION_MODE的方式。

如何檢查生成的lib是否正確:

可通過以下方式查看,打出的lib參數(shù)是-fembed-bitcode-marker還是-fembed-bitcode。
命令行輸入:

otool -arch armv7 -l xx.a/xx.framework

注意:如果lib包含了模擬器架構(gòu),則需要指定相應(yīng)真機的arch才能看得到。

Ctrl+F,搜索__bundle,應(yīng)該會出現(xiàn)以下內(nèi)容:

Section
sectname __bundle
segname __LLVM
addr 0x0002c000

 size 0x000b0a09

offset 180224
align 2^0 (1)
reloff 0
nreloc 0
flags 0x00000000
reserved1 0
reserved2 0

如果size不為1,則說明是-fembed-bitcode。

如果結(jié)果如下圖,則是-fembed-bitcode-marker,說明我們打的包不對。


image.png

另外,如果要查看lib是否支持bitcode,可執(zhí)行如下命令,查看是否有__LLVM。

otool -l xxxx | grep __LLVM | wc -l

如果輸出的數(shù)字不為0,就代表包含bitcode

framework使用過lipo命令,進行拆解和合并之后,需要指定指令集進行檢查才可以。

otool -arch armv7 -l xxxx | grep __LLVM | wc -l

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容