XCFramework
XCFramework是蘋果官方推薦的、支持的,可以更方便的表示一個多個平臺和架構(gòu)的分發(fā)二進(jìn)制的格式,需要XCode11以上。
制作靜態(tài)庫
模擬器架構(gòu)
先將工程編譯為 iphoneSimulator架構(gòu)
xcodebuild archive -project 'LEEAlert.xcodeproj' \
-scheme 'LEEAlert' \
-configuration Release \
-destination 'generic/platform=iOS Simulator' \
-archivePath '../archives/LEEAlert.framework-iphonesimulator.xcarchive' \
SKIP_INSTALL=NO
-
xcodebuild: 在Xcode中實際使用的命令。 -
archive: 打包。 -
project: 工程名。 -
scheme: 選擇 scheme。 -
configuration: 哪種環(huán)境下。 -
destination: 要分發(fā)的平臺,當(dāng)前指定的是 iOS Simulator。 -
archivePath: 壓縮之后,存放的路徑。 -
SKIP_INSTALL=NO:如果設(shè)置為YES,則不會將生成的framwork文件存放在Products目錄下。
真機(jī)架構(gòu)
接下來,我們來編譯真機(jī)的架構(gòu)
xcodebuild archive -project 'LEEAlert.xcodeproj' \
-scheme 'LEEAlert' \
-configuration Release \
-destination 'generic/platform=iOS' \
-archivePath '../archives/LEEAlert.framework-iphoneos.xcarchive' \
SKIP_INSTALL=NO
這樣我們就生成了模擬器架構(gòu)下和真機(jī)架構(gòu)下的打包文件

在 xcarchive文件中,在Product文件夾下,存放著相對應(yīng)的庫文件。

lipo命令合并
接下來,我們將這兩個架構(gòu)下的framework進(jìn)行合并,我們使用lipo命令
lipo -output LEEAlert -create ../archives/LEEAlert.framework-iphoneos.xcarchive/Products/Library/Frameworks/LEEAlert.framework/LEEAlert ../archives/LEEAlert.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/LEEAlert.framework/LEEAlert
復(fù)制代碼
-
-output: 輸出的名稱。 - 需要合并文件的路徑。
我們會遇到一個have the same architectures (arm64) and can't be in the same fat output file的錯誤
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: ../archives/LEEAlert.framework-iphoneos.xcarchive/Products/Library/Frameworks/LEEAlert.framework/LEEAlert and ../archives/LEEAlert.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/LEEAlert.framework/LEEAlert have the same architectures (arm64) and can't be in the same fat output file
復(fù)制代碼
這是因為模擬器架構(gòu)下的靜態(tài)庫有arm64,真機(jī)架構(gòu)下的靜態(tài)庫也有arm64,有相同的架構(gòu)導(dǎo)致不能合并。 我們將 x86_64 架構(gòu)從靜態(tài)庫文件中提取出來,這樣就保證了只有一種架構(gòu),不會重復(fù)。
lipo -output LEEAlert-x86_64 -extract x86_64 ../archives/LEEAlert.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/LEEAlert.framework/LEEAlert
然后我們在來進(jìn)行合并,在 archives的同目錄下,我們新建lipo文件夾,并將產(chǎn)出存放到該目錄。
bel@beldeMacBook-Pro lipo % lipo -output LEEAlert -create ../archives/LEEAlert.framework-iphoneos.xcarchive/Products/Library/Frameworks/LEEAlert.framework/LEEAlert ../LEEAlert/LEEAlert-x86_64
接下來,我們還需要給靜態(tài)庫文件配置頭文件和資源文件等信息,比較繁瑣。使用lipo命令來創(chuàng)建靜態(tài)庫存在兩個問題:
1,含有相同架構(gòu)的兩個靜態(tài)庫不能合并。
2,配置頭文件和資源文件比較繁瑣。
XCFramework
和傳統(tǒng)的framework相比:
- 1,可以用單個.xcframework文件提供多個平臺的分發(fā)二進(jìn)制文件;
- 2,與
Fat Header相比,可以按照平臺劃分,可以包含相同架構(gòu)的不同平臺文件。 - 3,在使用時,不需要再通過腳本去剝離不需要的架構(gòu)體系。
創(chuàng)建xcframework
接下來我們創(chuàng)建一個xcframework
bel@beldeMacBook-Pro xcframework % xcodebuild -create-xcframework \
-framework '../archives/LEEAlert.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/LEEAlert.framework' \
-debug-symbols '/Users/bel/Desktop/gitBox/Examples/archives/LEEAlert.framework-iphonesimulator.xcarchive/dSYMs/LEEAlert.framework.dSYM' \
-framework '../archives/LEEAlert.framework-iphoneos.xcarchive/Products/Library/Frameworks/LEEAlert.framework' \
-debug-symbols '/Users/bel/Desktop/gitBox/Examples/archives/LEEAlert.framework-iphoneos.xcarchive/BCSymbolMaps/0C1C6181-F2E0-3D17-8573-65CC6AEDBD97.bcsymbolmap' \
-debug-symbols '/Users/bel/Desktop/gitBox/Examples/archives/LEEAlert.framework-iphoneos.xcarchive/BCSymbolMaps/7D66A732-3121-386D-8397-5F44DEA908F1.bcsymbolmap' \
-debug-symbols '/Users/bel/Desktop/gitBox/Examples/archives/LEEAlert.framework-iphoneos.xcarchive/dSYMs/LEEAlert.framework.dSYM' \
-output 'LEEAlert.xcframework'
xcframework successfully written out to: /Users/bel/Desktop/gitBox/Examples/xcframework/LEEAlert.xcframework
復(fù)制代碼
-
-framework: framework的路徑 -
-debug-symbols:調(diào)試符號,必須為絕對路徑 -
-output: 輸出位置
這樣我們就生成了xcframework文件。
使用xcframework創(chuàng)建的靜態(tài)庫沒有出現(xiàn)含有重復(fù)架構(gòu)的情況,并且也有頭文件信息
在本例中我遇到了一個No 'swiftinterface' files found within的錯誤,這個需要將project文件中的 BUILD_FOR_LIBRARIES_FOR_DISTRIBUTION 設(shè)置為YES,然后,重新編譯,就可以了。
使用xcframework
新建一個工程,然后將 LEEAlert.xcframework加入到 Frameworks里面
然后,導(dǎo)入頭文件,就可以使用了
import UIKit
import LEEAlert
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
LYPerson.run()
}
}
LYXCFrameworkTe[7770:14236252] 正在跑步......
復(fù)制代碼
當(dāng)我們將文件拖入Xcode中,Xcode會根據(jù)我們運行的架構(gòu),選擇相對應(yīng)架構(gòu)的文件。如果我們運行的是模擬器,只會拷貝x86架構(gòu)的文件,如果運行的是真機(jī)設(shè)備,只會拷貝arm64架構(gòu)的文件,這樣可以減少App包的體積。
這樣我們就使用XCFramework來完成了靜態(tài)庫的制作,相比較于lipo,xcframework有幾個有點:
- 1,無需處理頭文件。
- 2,沒有重復(fù)架構(gòu)的問題。
- 3,在鏈接的時候,會自動選擇相應(yīng)的架構(gòu),無需全部拷貝至App,減小了App體積。
如果使用Xcode來制作靜態(tài)庫,有興趣的可以參考一下這篇文章XCode12制作Swift和OC混編靜態(tài)庫