最近几天研究了一下自动化打包,在网上四处搜刮了各种资料,最后揉吧揉吧写成了下面这样,跑了一下,可以用,所以把一些要点记下来作为备忘。
1.核心是xcodebuild的相关命令,参数很多,但是大体上套路就是三个:
清理 - clean
xcodebuild -workspace MyWorkspace.xcworkspace -scheme MyScheme
归档 -archive
xcodebuild archive -workspace MyWorkspace.xcworkspace -scheme MyScheme
导出 -exportArchive
xcodebuild -exportArchive -archivePath MyMobileApp.xcarchive -exportPath ExportDestination -exportOptionsPlist exportOptionsPlistName
这里面需要提一下的是 -exportOptionsPlist 这个参数,需要提供一个“ExportOptions.plist”文件。最简单的方式是使用xcode归档并且导出一个ipa,在目录下面就会自动生成这个文件,可以直接拷贝过来用。这里给出一个栗子:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>compileBitcode</key>
<false/>
<key>method</key>
<string>ad-hoc</string>
<key>signingStyle</key>
<string>automatic</string>
<key>stripSwiftSymbols</key>
<true/>
<key>teamID</key>
<string>3BH2YRLSPK</string>
<key>thinning</key>
<string><none></string>
</dict>
</plist>
其实就是各种导出参数,包括compileBitcode,method,teamID,thinning等等。
然后是一个完整的配置和导出包的shell脚本内容:
# 工程名
APP_NAME="iCarCustomer"
# 证书
CODE_SIGN_DISTRIBUTION="iPhone Distribution: Shanghai FengZhiXing Automobile Finance Information Service Co.,Ltd. "
# info.plist路径
project_infoplist_path="./${APP_NAME}/Info.plist"
#取版本号
bundleShortVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleShortVersionString" "${project_infoplist_path}")
#取build值
bundleVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleVersion" "${project_infoplist_path}")
DATE="$(date +%Y%m%d)"
IPANAME="${APP_NAME}_V${bundleShortVersion}_${DATE}.ipa"
ARCNAME="${APP_NAME}.xcarchive"
#要上传的ipa文件路径
IPA_PATH="/Users/hefei/Dev/Archive/${IPANAME}"
#要上传的ipa文件路径
ARC_PATH="/Users/hefei/Dev/Archive/${ARCNAME}"
ExportOptionsPlistPath="/Users/hefei/Dev/Archive/ExportOptions.plist"
# echo ${IPA_PATH}
echo "${IPA_PATH}">> text.txt
#下面2行是没有Cocopods的用法
echo "=================clean================="
xcodebuild -target "${APP_NAME}" -configuration 'Release' clean
echo "+++++++++++++++++build+++++++++++++++++"
# xcodebuild -target "${APP_NAME}" -sdk iphoneos -configuration 'Release' CODE_SIGN_IDENTITY="${CODE_SIGN_DISTRIBUTION}" SYMROOT='$(PWD)'
#xcodebuild -target -sdk iphoneos -configuration 'Release' SYMROOT='$(PWD)'
xcodebuild archive -project "${APP_NAME}.xcodeproj" -scheme "${APP_NAME}" archive -archivePath "${ARC_PATH}"
#下面2行是集成有Cocopods的用法
#echo "=================clean================="
#xcodebuild -workspace "${APP_NAME}.xcworkspace" -scheme "${APP_NAME}" -configuration 'Release' clean
#echo "+++++++++++++++++build+++++++++++++++++"
#xcodebuild -workspace "${APP_NAME}.xcworkspace" -scheme "${APP_NAME}" -sdk iphoneos -configuration 'Release' CODE_SIGN_IDENTITY="${CODE_SIGN_DISTRIBUTION}" SYMROOT='$(PWD)'
# xcrun -sdk iphoneos PackageApplication "./Release-iphoneos/${APP_NAME}.app" -o ~/"${IPANAME}"
xcodebuild -exportArchive -archivePath "${ARC_PATH}" -exportPath "${IPA_PATH}" -exportOptionsPlist "${ExportOptionsPlistPath}" -allowProvisioningUpdates
这个脚本是在网上找的,按照自己的需要修改成上面的样子,亲测可用,其实已经写得很明白了,命令基本不需要改,按照自己的场景配置参数就可以了。
下面的步骤就是打完包把包传到APPStore或者是其他的分享平台,比如fir等等。
APPStore参数需要单独配置,现在以ad-hoc的方式来上传fir平台。
1.在命令行模式下安装fir-cli 注意安装这个需要制定另外的目录,否则会出现写权限错误(ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /usr/bin directory.)
sudo gem install -n /usr/local/bin fir-cli
之后在上传脚本之后执行
# 将XXX替换成自己的Fir平台的token
fir login -T XXX
fir publish $exportIpaPath/$scheme_name.ipa