场景
framework的开发者开发完成后,通常需要构建真机、模拟器版本,然后合并架构,再分发给framework的调用者调用。与手工构建相比,自动化的构建脚本让这个过程更加高效。
前序知识
-
shell
教程:Shell 基础教程
用途:构建脚本即为Shell语法编写的脚本程序,需要掌握Shell基础语法。
-
PlistBuddy
简介:PlistBuddy是Mac系统提供的一个用于操作plist文件的命令行工具,路径:
/usr/libexec/PlistBuddy
用途:修改Xcode工程中info.plist,设置CFBundleVersion、CFBundleShortVersionString版本信息。
语法:
/usr/libexec/PlistBuddy -c 'Set :$key string "value" ' info.plist
-
xcodebuild
简介:xcodebuild是Xcode提供的一套命令行构建工具,它能构建Xcode projcect下的一个或者多个targets ,也能构建一个workspace或Xcode project下的scheme。
用途:编译工程
语法:
# project target xcodebuild -project <your project>.xcodeproj \ -target <your target> \ -configuration Release \ -sdk iPhoneOS # workspace scheme xcodebuild -workspace <your workspace>.xcworkspace \ -scheme <your scheme> \ -configuration Release \ -sdk iPhoneOS
-
lipo
简介:lipo是一个创建或操作universal File的工具, 可以查看架构信息, 提取特定架构,整合和拆分库文件。
用途:合并真机、模拟器版本的framework
语法
lipo -create libA.a libB.a -output libC.a
-
zip
简介:一个压缩工具
用途:压缩framework
语法:
zip -r -m -o a.zip a
构建脚本
基于project target
#!/bin/sh
#参数
project_name="<your project name>"
framework_name=${project_name}
build_dir="build"
out_dir="out"
# 清除缓存
function build_clean {
echo "======build_clean======"
rm -rf ${build_dir}
rm -rf ${out_dir}
}
# 配置
function build_config {
echo "======build_config======"
# build版本号:日期
build_version=`date +%y%m%d%H%M%S`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${build_version}" ./${project_name}/info.plist
# build版本号:自增
# build_version=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "./${project_name}/info.plist" )
# build_version=$(($build_version + 1))
# /usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${build_version}" ./${project_name}/info.plist
}
# 编译
function build_framework {
echo "======build_framework======"
# 若支持bitCode,需加参数:-fembed-bitcode
xcodebuild -project ${project_name}.xcodeproj -sdk iphoneos -configuration "Release" OTHER_CFLAGS="-fembed-bitcode" BUILD_DIR="./${build_dir}" build || exit 1
xcodebuild -project ${project_name}.xcodeproj -sdk iphonesimulator -configuration "Release" OTHER_CFLAGS="-fembed-bitcode" BUILD_DIR="./${build_dir}" build || exit 1
}
# 合并
function build_fat_framework {
echo "======build_fat_framework======"
mkdir -p ${out_dir}
cp -R ${build_dir}/Release-iphoneos/${framework_name}.framework ${out_dir}/
lipo -create ${build_dir}/Release-iphonesimulator/${framework_name}.framework/${framework_name} ${build_dir}/Release-iphoneos/${framework_name}.framework/${framework_name} -output ${out_dir}/${framework_name}.framework/${framework_name} || exit 1
}
# 压缩
function build_zip {
echo "======build_zip======"
cd ${out_dir}
time=`date +%y%m%d%H%M%S`
name=${framework_name}_${time}.zip
zip -r -m -o ${framework_name}.framework.zip ${framework_name}.framework || exit 1
}
# 调用
build_clean
build_config
build_framework
build_fat_framework
build_zip
基于workspace scheme
#!/bin/sh
# 参数
workspace="<your workspace>"
scheme="<your scheme>"
project_name=${scheme}
framework_name=${scheme}
build_dir="build"
out_dir="out"
# 清除缓存
function build_clean {
echo "======build_clean======"
rm -rf ${build_dir}
rm -rf ${out_dir}
}
# 配置
function build_config {
echo "======build_config======"
# build版本号:日期
build_version=`date +%y%m%d%H%M%S`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${build_version}" ./${project_name}/${framework_name}/info.plist
# build版本号:自增
# build_version=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "./${project_name}/${framework_name}/info.plist" )
# build_version=$(($build_version + 1))
# /usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${build_version}" ./${project_name}/${framework_name}/info.plist
}
# 编译
function build_framework {
echo "======build_framework======"
#若支持bitCode,需加参数:-fembed-bitcode
xcodebuild -workspace ${workspace}.xcworkspace -scheme ${scheme} -sdk iphoneos -configuration "Release" OTHER_CFLAGS="-fembed-bitcode" BUILD_DIR="../${build_dir}" build || exit 1
xcodebuild -workspace ${workspace}.xcworkspace -scheme ${scheme} -sdk iphonesimulator -configuration "Release" OTHER_CFLAGS="-fembed-bitcode" BUILD_DIR="../${build_dir}" build || exit 1
}
# 合并
function build_fat_framework {
echo "======build_fat_framework======"
mkdir -p ${out_dir}/
cp -R ${build_dir}/Release-iphoneos/${framework_name}.framework ${out_dir}/
lipo -create ${build_dir}/Release-iphonesimulator/${framework_name}.framework/${framework_name} ${build_dir}/Release-iphoneos/${framework_name}.framework/${framework_name} -output ${out_dir}/${framework_name}.framework/${framework_name} || exit 1
}
# 压缩
function build_zip {
echo "======build_zip======"
cd ${out_dir}
time=`date +%y%m%d%H%M%S`
name=${framework_name}_${time}.zip
zip -r -m -o ${framework_name}.framework.zip ${framework_name}.framework || exit 1
}
# 调用
build_clean
build_config
build_framework
build_fat_framework
build_zip
总结
-
framework构建脚本主要使用了PlistBuddy、xcodebuild、lipo、zip等命令行工具,需要熟悉各个命令的基本用法,也需要掌握Shell基础语法,以便根据自身使用场景对脚本进行定制修改;
-
关于bitcode:Enable Bitcode 设置为 YES,进行Archive时,bitcode会被嵌入到链接后的二进制文件中。进行其他类型的build(非Archive)时,编译器只会检查是否满足开启bitcode的条件,但并不会真正生成bitcode,需要显式增加参数-fembed-bitcode。
今天的文章iOS framework xcodebuild 构建脚本分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/15916.html