编写 iOS 动态库自动打包脚本

动态库介绍:
一个标准的 iOS 第三方库一般会包含以下几类文件:

.framework 文件其实是一个文件夹,里面还包含 plist 文件和 Unix 执行文件等。所有这些文件都是通过 xcodebuild 编译生成。
动态库编译:
编译真机库:
xcodebuild \

DEPLOYMENT_LOCATION=YES \
TARGETED_DEVICE_FAMILY=1 \
ONLY_ACTIVE_ARCH=NO \
BITCODE_GENERATION_MODE=bitcode \
-workspace $FRAMEWORK.xcworkspace \
-scheme $FRAMEWORK \
-configuration release \
-derivedDataPath $DERIVEDDATA_PATH \
-sdk iphoneos | xcpretty
编译模拟器库:
xcodebuild \

DEPLOYMENT_LOCATION=YES \
TARGETED_DEVICE_FAMILY=1 \
ONLY_ACTIVE_ARCH=NO \
BITCODE_GENERATION_MODE=bitcode \
-workspace $FRAMEWORK.xcworkspace \
-scheme $FRAMEWORK \
-configuration release \
-derivedDataPath $DERIVEDDATA_PATH \
-sdk iphonesimulator | xcpretty
执行完以上命令,会分别生生成模拟器和真机使用的动态库,只有真机库编译才会产生bcsymbolmap文件,可以用 lipo 确认一下它们包含的可执行文件所支持的平台:
lipo -i 可执行文件路径
新建一个目录,把真机器的库文件复制过去:
cp -R $PRODCUTS_PATH/Release-iphoneos/$FRAMEWORK.framework.dSYM $PRODCUTS_PATH/Release-universal/$FRAMEWORK.framework.dSYM

cp -RL $PRODCUTS_PATH/Release-iphoneos/$FRAMEWORK.framework $PRODCUTS_PATH/Release-universal/$FRAMEWORK.framework
find "$PRODCUTS_PATH/Release-iphoneos" -name '*.bcsymbolmap' -type f -exec cp -pv {} "$PRODCUTS_PATH/Release-universal" \;
修改 plist,更新库版本:
plutil -replace CFBundlePackageType -string FMWK "$PRODCUTS_PATH/Release-universal/$FRAMEWORK.framework/Info.plist"

plutil -replace CFBundleShortVersionString -string ${CIRCLE_TAG#v} "$PRODCUTS_PATH/Release-universal/$FRAMEWORK.framework/Info.plist"
plutil -replace CFBundleVersion -string $CIRCLE_BUILD_NUM "$PRODCUTS_PATH/Release-universal/$FRAMEWORK.framework/Info.plist"
由于我们最终只能有一个可执行文件,需要对可执行文件进行合并:
lipo -create \

$PRODCUTS_PATH/Release-iphoneos/$FRAMEWORK.framework/$FRAMEWORK \
$PRODCUTS_PATH/Release-iphonesimulator/$FRAMEWORK.framework/$FRAMEWORK \
-output $PRODCUTS_PATH/Release-universal/$FRAMEWORK.framework/$FRAMEWORK
最终压缩成 zip 文件:
cd $PRODCUTS_PATH/Release-universal

zip -yr $PACKAGE_FILE_NAME *
大功告成!🎉

参考资料: