最近開發(fā)SDK時在項目中使用靜態(tài)庫的時候都會有兩個版本,一個用于模擬器柑爸,一個用于真機吵护,這樣就比較麻煩表鳍。為了模擬器與真機之間切換調(diào)試的方便,需要制作通用版的Framework交叉包譬圣。
根據(jù)Build時選擇的機器類型,會分為模擬器Framework和真機Framework厘熟,兩者是不能混用的屯蹦。此時可以通過配置一個Run Script,在Script中使用lipo命令來合并兩個版本的Framework登澜,重新生成一個新的Framework,這個Framework將同時支持在模擬器和真機上運行脑蠕。
1、檢查Framework包信息
這個兩個目錄下包含了生成的Framework文件夾谴仙,第一個是真機、第二個是模擬器(當(dāng)前使用的是Debug模式狞甚,也可以使用Release模式)锁摔。按照預(yù)期:真機的包是顯示armv7 arm64 架構(gòu)的哼审,模擬器的包是顯示x86_64架構(gòu)的。
現(xiàn)在我們分別對這兩個目錄中的Framework進(jìn)行檢測:
1)涩盾、Debug-iphoneos(真機)目錄下的Framework:
MiniPC:~ lijl$ lipo -info /Users/lijl/Library/Developer/Xcode/DerivedData/SwiftCommon-dyhotzllcfgqjibahyytumthguvh/Build/Products/Debug-iphoneos/SwiftCommon.framework/SwiftCommon
Architectures in the fat file: /Users/lijl/Library/Developer/Xcode/DerivedData/SwiftCommon-dyhotzllcfgqjibahyytumthguvh/Build/Products/Debug-iphoneos/SwiftCommon.framework/SwiftCommon are: armv7 arm64
MiniPC:~ lijl$
//注:Framework是文件夾,命令再往下一層春霍,使用SDK的二進(jìn)制文件
真機Framework的結(jié)果是:armv7 arm64≈啡澹可見芹枷,如果把這個Framework運行在模擬器中莲趣,肯定會報錯,因為模擬器架構(gòu)是x86_64的喧伞。
2)、Debug-iphonesimulator(模擬器)目錄下的Framework:
MiniPC:~ lijl$ lipo -info /Users/lijl/Library/Developer/Xcode/DerivedData/SwiftCommon-dyhotzllcfgqjibahyytumthguvh/Build/Products/Debug-iphonesimulator/SwiftCommon.framework/SwiftCommon
Non-fat file: /Users/lijl/Library/Developer/Xcode/DerivedData/SwiftCommon-dyhotzllcfgqjibahyytumthguvh/Build/Products/Debug-iphonesimulator/SwiftCommon.framework/SwiftCommon is architecture: x86_64
MiniPC:~ lijl$
模擬器Framework的結(jié)果是x86_64潘鲫。可見這個庫只能運行在模擬器中溉仑,真機設(shè)備是無法運行的挖函。
2彼念、創(chuàng)建腳本打交叉包
接下來浅萧,我們要對這2個庫(其實是Framework目錄下2個可執(zhí)行文件)進(jìn)行合并。
在項目的Build Phases中洼畅,新建一個Run Script,輸入下面內(nèi)容:
if [ "${ACTION}" = "build" ]
then
INSTALL_DIR=${SRCROOT}/Products/${PROJECT_NAME}.framework
DEVICE_DIR=${BUILD_ROOT}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework
SIMULATOR_DIR=${BUILD_ROOT}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
mkdir -p "${INSTALL_DIR}"
cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"
#ditto "${DEVICE_DIR}/Headers" "${INSTALL_DIR}/Headers"
lipo -create "${DEVICE_DIR}/${PROJECT_NAME}" "${SIMULATOR_DIR}/${PROJECT_NAME}" -output "${INSTALL_DIR}/${PROJECT_NAME}"
#open "${DEVICE_DIR}"
#open "${SRCROOT}/Products"
fi
經(jīng)過很多測試帝簇,下面這個修改后的Run Script會更加通用,不會出錯靠益,唯一報錯的原因是對應(yīng)目錄下的包沒有生成,這段shell代碼其實很簡單胧后,前面一大部分是申明一些目錄路徑,最后使用lipo命令進(jìn)行合并操作壳快。
3纸巷、創(chuàng)建聚合Target生成的交叉包
1眶痰、創(chuàng)建target,選擇Other下的Aggregate
2竖伯、嵌入腳本。選中剛剛創(chuàng)建的Aggregate,然后選中右側(cè)的Build Phases,點擊左下方加號七婴,選擇New Run Script Phase祟偷。
3打厘、新建腳本:
# define output folder environment variable
CONFIGURATION=Release
UNIVERSAL_OUTPUTFOLDER_NATIVE=${SRCROOT}/${CONFIGURATION}-build/${PROJECT_NAME}.framework
#build support native API
# Step 1. Build Device and Simulator versions
cd "${SRCROOT}/.."
xcodebuild build -workspace ${PROJECT_NAME}.xcworkspace -scheme ${PROJECT_NAME} ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION_RN} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"
xcodebuild build -workspace ${PROJECT_NAME}.xcworkspace -scheme ${PROJECT_NAME} -configuration ${CONFIGURATION} -sdk iphonesimulator VALID_ARCHS="i386 x86_64" BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"
# make sure the output directory exists
rm -rf "${SRCROOT}/${CONFIGURATION}-build"
mkdir -p "${SRCROOT}/${CONFIGURATION}-build"
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/" "${UNIVERSAL_OUTPUTFOLDER_NATIVE}/"
# Step 2. Create universal binary file using lipo
lipo -create "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" -output "${UNIVERSAL_OUTPUTFOLDER_NATIVE}/${PROJECT_NAME}"
# Last touch. copy the header files. Just for convenience
補充更新,兼容xcode13:
# Type a script or drag a script file from your workspace to insert its path.
# 1、定義輸出變量
### 定義workspace名稱
WORKSPACE= xxx1
### 定義輸出SDK名稱
SDKTARGET=xxxSDK
### 定義輸出資源包名稱
BUNDLETARGET=xxxResources
### 定義編譯模式
CONFIGURATION=Release
### 定義輸出文件夾
PRODUCTS_OUTPUTFOLDER=${SRCROOT}/../Products
# 2魂爪、 先清理 TARGETS
#xcodebuild clean -target ${SDKTARGET}
#xcodebuild clean -target ${BUNDLETARGET}
cd "${SRCROOT}/.."
# 3先舷、 執(zhí)行編譯SDK TARGETS
xcodebuild -workspace ${WORKSPACE}.xcworkspace -scheme ${SDKTARGET} ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" -UseModernBuildSystem=NO
xcodebuild -workspace ${WORKSPACE}.xcworkspace -scheme ${SDKTARGET} -configuration ${CONFIGURATION} -sdk iphonesimulator VALID_ARCHS="i386 x86_64" BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" -UseModernBuildSystem=NO
# 4蒋川、 執(zhí)行編譯資源 TARGETS
xcodebuild -workspace ${WORKSPACE}.xcworkspace -scheme ${BUNDLETARGET} ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" -UseModernBuildSystem=NO
xcodebuild -workspace ${WORKSPACE}.xcworkspace -scheme ${BUNDLETARGET} -configuration ${CONFIGURATION} -sdk iphonesimulator BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" -UseModernBuildSystem=NO
# 5、 清空輸出指定文件夾撩笆、創(chuàng)建新文件夾
rm -rf "${PRODUCTS_OUTPUTFOLDER}"
mkdir -p "${PRODUCTS_OUTPUTFOLDER}"
# 6缸浦、 將編譯好的SDK和資源BUNDLE拷貝到指定輸出文件夾
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${SDKTARGET}.framework" "${PRODUCTS_OUTPUTFOLDER}/"
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${BUNDLETARGET}.bundle" "${PRODUCTS_OUTPUTFOLDER}/"
rm "${PRODUCTS_OUTPUTFOLDER}/${BUNDLETARGET}.bundle/${BUNDLETARGET}"
rm "${PRODUCTS_OUTPUTFOLDER}/${BUNDLETARGET}.bundle/Info.plist"
# 7、 合成真機和摸機器指令包
lipo -create "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${SDKTARGET}.framework/${SDKTARGET}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${SDKTARGET}.framework/${SDKTARGET}" -output "${PRODUCTS_OUTPUTFOLDER}/${SDKTARGET}.framework/${SDKTARGET}"
# 8裂逐、 打開輸出文件夾
open ${PRODUCTS_OUTPUTFOLDER}
4、驗證腳本生成的交叉包
現(xiàn)在卜高,當(dāng)項目再次Build時,就會在項目目錄下面出現(xiàn)Products目錄南片,里面包含了合并后的Framework文件夾。使用lipo -info來檢測下:
1疼进、打開終端薪缆,進(jìn)入到你的FrameWork伞广,cd xxxx/xxx.framework
2、查看架構(gòu)支持赔癌,lipo -info xxxx/xxx.framework/xxx (注意:xxx是你的FrameWork名稱)
MiniPC:~ lijl$ lipo -info /Users/lijl/Documents/Self\ Project/Cocoa\ Touch\ Framework/SwiftCommon/Products/SwiftCommon.framework/SwiftCommon
運行之后:
Architectures in the fat file: /Users/lijl/Documents/Self Project/Cocoa Touch Framework/SwiftCommon/Products/SwiftCommon.framework/SwiftCommon are: x86_64 armv7 arm64
MiniPC:~ lijl$
信息顯示成功的集成了 x86_64 armv7 arm64 ,這樣就生成了真機與模擬器通用的交叉包
附指令集:
設(shè)備的CPU架構(gòu)(指令集):
模擬器:
iPhone 4s~5: i386
iPhone 5s~最新: x86_64
真機(iOS設(shè)備):
armv6: iPhone灾票、iPhone 2、iPhone 3G刊苍、iPod Touch(第一代)既们、iPod Touch(第二代)
armv7: iPhone 3Gs正什、iPhone 4、iPhone 4s婴氮、iPad斯棒、iPad 2
armv7s: iPhone 5主经、iPhone 5c
arm64: iPhone 5s、iPhone 6罩驻、iPhone 6 Plus、iPhone 6s、iPhone 6s Plus砾跃、iPad Air骏啰、iPad Air2抽高、iPad mini2、iPad mini3厨内,以及最新的機型。