app上架時如果SDK里是支持模擬器架構(gòu)的位仁,蘋果是不讓上架的晒杈。甚至于嫂伞,打包成功后,導(dǎo)出為測試包都會報錯桐智。為了正常上架和測試末早,我們需要將SDK里的模擬器架構(gòu)給刪除。
方法一: xcode里添加腳本说庭,打包自動刪除模擬器架構(gòu)
這種方法不會刪除SDK源文件的架構(gòu)然磷,只有打的包被刪除,不會影響我們平時在模擬器上調(diào)試刊驴,所以推薦這種方式姿搜。
選擇添加 New Run Script Phase
然后添加一段腳本,這樣我們打包的時候就會自動將SDK里的支持模擬器架構(gòu)刪除掉了寡润。
#!/bin/sh
# Strip invalid architectures
strip_invalid_archs() {
binary="$1"
echo "current binary ${binary}"
# Get architectures for current file
archs="$(lipo -info "$binary" | rev | cut -d ':' -f1 | rev)"
stripped=""
for arch in $archs; do
if ! [[ "${ARCHS}" == *"$arch"* ]]; then
if [ -f "$binary" ]; then
# Strip non-valid architectures in-place
lipo -remove "$arch" -output "$binary" "$binary" || exit 1
stripped="$stripped $arch"
fi
fi
done
if [[ "$stripped" ]]; then
echo "Stripped $binary of architectures:$stripped"
fi
}
APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}"
# This script loops through the frameworks embedded in the application and
# removes unused architectures.
find "$APP_PATH" -name '*.framework' -type d | while read -r FRAMEWORK
do
FRAMEWORK_EXECUTABLE_NAME=$(defaults read "$FRAMEWORK/Info.plist" CFBundleExecutable)
FRAMEWORK_EXECUTABLE_PATH="$FRAMEWORK/$FRAMEWORK_EXECUTABLE_NAME"
echo "Executable is $FRAMEWORK_EXECUTABLE_PATH"
strip_invalid_archs "$FRAMEWORK_EXECUTABLE_PATH"
done
方法二:終端運(yùn)行腳本,刪除指定文件夾內(nèi)的SDK模擬器架構(gòu)
這種方式會刪除原SDK的架構(gòu)舅柜,所以適合用在對此SDK做二次封裝梭纹,提供給如跨平臺的前端使用。
我下載了一個網(wǎng)易云信的SDK
打開終端
cd /Users/apple/Downloads/NIMSDK/NIMSDK.framework
切換到該SDK目錄下
lipo -info NIMSDK
查詢這個framework支持的CPU架構(gòu)
可以看到支持i386 x86_64 armv7 arm64這四種架構(gòu)
模擬器32位處理器測試需要i386架構(gòu)致份,
模擬器64位處理器測試需要x86_64架構(gòu)变抽,
真機(jī)32位處理器需要armv7,或者armv7s架構(gòu),
真機(jī)64位處理器需要arm64架構(gòu)氮块。
在終端里 cd 到這個framework的上一層文件夾路徑下绍载,回車。
粘貼下面這段腳本到終端里執(zhí)行滔蝉。
# This script loops through the frameworks embedded in the application and
# removes unused architectures.
find . -name '*.framework' -type d | while read -r FRAMEWORK
do
#FRAMEWORK_EXECUTABLE_NAME=$(defaults read "$FRAMEWORK/Info.plist" CFBundleExecutable)
FRAMEWORK_EXECUTABLE_NAME=$(/usr/libexec/PlistBuddy -c "Print :CFBundleExecutable" "$FRAMEWORK/Info.plist")
echo $FRAMEWORK_EXECUTABLE_NAME
FRAMEWORK_EXECUTABLE_PATH="$FRAMEWORK/$FRAMEWORK_EXECUTABLE_NAME"
echo "Executable is $FRAMEWORK_EXECUTABLE_PATH"
echo $(lipo -info "$FRAMEWORK_EXECUTABLE_PATH")
FRAMEWORK_TMP_PATH="$FRAMEWORK_EXECUTABLE_PATH-tmp"
# remove simulator's archs if location is not simulator's directory
case "${TARGET_BUILD_DIR}" in
*"iphonesimulator")
echo "No need to remove archs"
;;
*)
if $(lipo "$FRAMEWORK_EXECUTABLE_PATH" -verify_arch "i386") ; then
lipo -output "$FRAMEWORK_TMP_PATH" -remove "i386" "$FRAMEWORK_EXECUTABLE_PATH"
echo "i386 architecture removed"
rm "$FRAMEWORK_EXECUTABLE_PATH"
mv "$FRAMEWORK_TMP_PATH" "$FRAMEWORK_EXECUTABLE_PATH"
fi
if $(lipo "$FRAMEWORK_EXECUTABLE_PATH" -verify_arch "x86_64") ; then
lipo -output "$FRAMEWORK_TMP_PATH" -remove "x86_64" "$FRAMEWORK_EXECUTABLE_PATH"
echo "x86_64 architecture removed"
rm "$FRAMEWORK_EXECUTABLE_PATH"
mv "$FRAMEWORK_TMP_PATH" "$FRAMEWORK_EXECUTABLE_PATH"
fi
;;
esac
echo "Completed for executable $FRAMEWORK_EXECUTABLE_PATH"
echo $(lipo -info "$FRAMEWORK_EXECUTABLE_PATH")
done
執(zhí)行后終端顯示如下則表示刪除成功
此時再查詢framework支持的架構(gòu)击儡,只剩下armv7和arm64了。
可能出現(xiàn)的問題:
在實(shí)際操作中蝠引,有可能出現(xiàn)有些framework不能執(zhí)行成功阳谍。
就拿AlipaySDK舉例,在對AlipaySDK執(zhí)行這段腳本時發(fā)生了錯誤螃概。
瀏覽一下錯誤輸出矫夯,大致意思為找不到Info.plist文件,也找不到可執(zhí)行文件路徑谅年。我們來看一下它的文件茧痒,發(fā)現(xiàn)問題出在了plist文件名上,腳本里是查找名為Info.plist的文件融蹂,但是AlipaySDK的plist名稱為AlipaySDK-inside-Info.plist
我們先將AlipaySDK-inside-Info.plist文件拷貝出來旺订,完成操作后再拿回來。
下面先將AlipaySDK-inside-Info.plist改名為Info.plist
打開這個Info.plist文件
此時文件里的 Executable file 對應(yīng)的值為 ${EXECUTABLE_NAME} 超燃,我們需要把它改為真正的可執(zhí)行文件名稱区拳,也就是下圖中的AlipaySDK
修改完畢如下圖:
再次執(zhí)行腳本成功!