一、说明
最近需要上传appstroe,每次长传都要等待很久,于是就在网上找自动上传的资料,方法有好几种Jenkins、fastlane,应为只是简单的上传appstroe,最后选择了使用xcodebuild。
参考文章找了很久没找到,如有看到提醒我加上。
使用前需要安装安装xcodebuild
二、步骤简单介绍(最后代码中已有注释)
1.清除前面生成的编译、打包文件
xcodebuild clean -workspace ${Project_Name}.xcworkspace -scheme ${Project_Name} -configuration ${Configuration}
2.编译
xcodebuild archive \ -workspace ${Project_Name}.xcworkspace -scheme ${Project_Name} -configuration ${Configuration} -archivePath ${APPSTORE_PATH}/${Project_Name}-appstore.xcarchive
3.打ipa包,自动证书需要加上-allowProvisioningUpdates
xcodebuild -exportArchive \ -archivePath ${APPSTORE_PATH}/${Project_Name}-appstore.xcarchive -exportOptionsPlist ${AppStoreExportOptionsPlist} -exportPath ${AppStorePrijectOutPath} -allowProvisioningUpdates
4.使用altool
验证和上传ipa包
初步验证ipa包,这里我需要验证包是否初步验证成功需要,将验证结果导出到plist中> ${APPSTORE_PATH}/validateInfo.plist
,如果验证成功会有一个成功的keysuccess-message
获取plist中的key-value语法/usr/libexec/PlistBuddy -c "Print success-message" ${APPSTORE_PATH}/validateInfo.plist
苹果官网术语表
xcrun altool --validate-app -f file -t platform -u username [-p password] [--output-format xml]
xcrun altool --upload-app -f file -t platform -u username [-p password] [--output-format xml]
5.最后是xcode中版本号自增,用到agvtool
5.1.agvtool next-version -all
就能自动增加build号了,但是是整数,我这里直接用的整数,如果需要像x.x.x
这样,可以参考下面代码中设置bundle identifier
5.2.获取当前版本的keyCFBundleShortVersionString
/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" ${Project_Name}/Info.plist
5.3.设置版本号
/usr/libexec/PlistBuddy -c "Set CFBundleShortVersionString $newVersionStr" "${Project_Name}/Info.plist"
三、使用步骤
1.在.xcodeproj文件夹下创建shell.sh
文件、ExportOptions.plist
文件,ExportOptions.plist
可以自己先手动打包一次,就会有生产这个plist
2.把下面代码复制到.sh中,Workspace_Name、Project_Name、AppleID 与 密码
3.xcode需要设置target -> info -> Bundle version和Bundle version string (short)用于版本自增
4.终端cd到.xcodeproj文件夹下、执行./shell.sh
#workspace的名字,如果没有则不需要这段话,我测试工程不是一个工作空间所以就没用,如果需要用的话,还需要在编译阶段的脚本代码里加上 -workspace ${Project_Name}.xcworkspace
Workspace_Name="项目bundle identifier"
#工程名字
Project_Name="项目bundle identifier"
#配置打包方式Release或者Debug
Configuration="Release"
#基础主路径
BUILD_PATH=./build
#不同版本的基础子路径
#adHoc
ADHOC_PATH=${BUILD_PATH}/adHoc
#appStore
APPSTORE_PATH=${BUILD_PATH}/appStore
#配置编译文件的存放地址
#adHoc
CONFIGURATION_BUILD_PATH_ADHOC=${ADHOC_PATH}/${Configuration}-iphoneos
#appStore
CONFIGURATION_BUILD_PATH_APPSTORE=${APPSTORE_PATH}/${Configuration}-iphoneos
#配置打包结果输出的路径
#AdHoc版本
AdHocPrijectOutPath=${ADHOC_PATH}/adHocOut
#AppStore版本
AppStorePrijectOutPath=${APPSTORE_PATH}/appStoreOut
#上传appstroe和ad-hoc配置不同,需要自行配置
ADHOCExportOptionsPlist="./ExportOptions.plist"
AppStoreExportOptionsPlist="./ExportOptions.plist"
#在终端中提示 根据输入的序号不同,打包成不同版本的ipa
echo "~~~~~~~~~~~~选择打包方式(输入序号)~~~~~~~~~~~~~~~"
echo " 1 adHoc"
echo " 2 AppStore"
# 读取用户在终端中输入并存到变量里
read parameter
sleep 0.5
method="$parameter"
# 判读用户是否有输入
if [ -n "$method" ]
then
#clean 每次先清空之前打包好的文件
xcodebuild clean \
-workspace ${Project_Name}.xcworkspace \
-scheme ${Project_Name} \
-configuration ${Configuration}
if [ "$method" = "1" ]
then
#这里都执行命令中是在xcworkspace工程中执行的,如果工程不是xcworkspace,可以把-workspace的内容删掉,加入了证书和描述文件,如果不需要请删除
#如果用户选择的是1,就执行adhoc脚本
rm -rf ${BUILD_PATH}
#创建文件夹,路径需要一层一层创建,不然会创建失败
mkdir ${BUILD_PATH}
mkdir ${ADHOC_PATH}
#编译文件
mkdir ${CONFIGURATION_BUILD_PATH_ADHOC}
#打包输出的文件
mkdir ${AdHocPrijectOutPath}
#copy
mkdir ${DSYM_COPY_PATH_ADHOC}
#编译
#自动证书配置需要加上这个-allowProvisioningUpdates
xcodebuild archive \
-workspace ${Project_Name}.xcworkspace \
-scheme ${Project_Name} \
-configuration ${Configuration} \
-archivePath ${ADHOC_PATH}/${Project_Name}-adhoc.xcarchive \
-allowProvisioningUpdates \
#打包
xcodebuild -exportArchive -archivePath ${ADHOC_PATH}/$Project_Name-adhoc.xcarchive -exportOptionsPlist $ADHOCExportOptionsPlist -exportPath ${AdHocPrijectOutPath} -allowProvisioningUpdates
elif [ "$method" = "2" ]
then
rm -rf ${BUILD_PATH}
#创建文件夹,路径需要一层一层创建,不然会创建失败
mkdir ${BUILD_PATH}
mkdir ${APPSTORE_PATH}
#编译文件
mkdir ${CONFIGURATION_BUILD_PATH_APPSTORE}
#打包输出的文件
mkdir ${AppStorePrijectOutPath}
#xcarchive
mkdir ${DSYM_COPY_PATH_APPSTORE}
xcodebuild archive \
-workspace ${Project_Name}.xcworkspace \
-scheme ${Project_Name} \
-configuration ${Configuration} \
-archivePath ${APPSTORE_PATH}/${Project_Name}-appstore.xcarchive \
#ipa
#自动证书配置需要加上这个-allowProvisioningUpdates
xcodebuild -exportArchive \
-archivePath ${APPSTORE_PATH}/${Project_Name}-appstore.xcarchive \
-exportOptionsPlist ${AppStoreExportOptionsPlist} \
-exportPath ${AppStorePrijectOutPath} \
-allowProvisioningUpdates
#验证ipa是否打包成功
if [ -e $AppStorePrijectOutPath/$Project_Name.ipa ]; then
echo '----ipa包已生成----'
open $AppStorePrijectOutPath
echo '----打包ipa完成----'
echo '**---------------**'
echo '****开始发布ipa包****'
echo '**---------------**'
#验证后上传到App Store
# 将-u 后面的XXX替换成自己的AppleID的账号,-p后面的XXX替换成自己的密码(不是登录密码,需要生成)
altoolPath="/Applications/Xcode.app/Contents/Developer/usr/bin/altool"
#(> ${APPSTORE_PATH}/validateInfo.plist)是把返回内容输出到plist文件内
xcrun "$altoolPath" --validate-app -f ${AppStorePrijectOutPath}/${Project_Name}.ipa -u xxx -p xxx -t ios --output-format xml > ${APPSTORE_PATH}/validateInfo.plist
if [ -e $AppStorePrijectOutPath/$Project_Name.ipa ]; then
#判断是否认证成功
successKey=$(/usr/libexec/PlistBuddy -c "Print success-message" ${APPSTORE_PATH}/validateInfo.plist)
if [ "$successKey" = "" ]
then
echo "--validate-app认证失败"
else
echo "--validate-app认证成功"
xcrun "$altoolPath" --upload-app -f ${AppStorePrijectOutPath}/${Project_Name}.ipa -u xxx -p xxx -t ios --output-format xml
increaseVersionNumberAndBuildNumber
fi
else
echo '----ipa包导出失败----'
fi
increaseVersionNumberAndBuildNumber() {
agvtool next-version -all
versionNum=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" ${Project_Name}/Info.plist)
thirdPartVersonNum=`echo $versionNum | awk -F "." '{print $3}'`
thirdPartVersonNum=$(($thirdPartVersonNum + 1))
newVersionStr=`echo $versionNum | awk -F "." '{print $1 "." $2 ".'$thirdPartVersonNum'" }'`
/usr/libexec/PlistBuddy -c "Set CFBundleShortVersionString $newVersionStr" "${Project_Name}/Info.plist"
}
这里我只全流程走了选项2的上传appstroe,adhoc没有试完。
希望对你有帮助
完!