-
xcodebuild
主要是用來編譯悉抵,打包成Archive和導出ipa包。 - 官方文檔 Workspace姥饰、Project孝治、Scheme、Target的區(qū)別
-
altool
提交到App Store
使用 - altool使用官方文檔第38頁
- altool 這個工具實際上是ApplicationLoader岂座,打開Xcode-左上角Xcode-Open Developer Tool-Application Loader
- altool的路徑是:
/Applications/Xcode.app/Contents/Applications/Application\ Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Support/altool
- 使用時如報如下錯誤
altool[] *** Error:
Exception while launching iTunesTransporter: Transporter not found at path: /usr/local/itms/bin/iTMSTransporter.
You should reinstall the application.
- 建立軟鏈
ln -s /Applications/Xcode.app/Contents/Applications/Application\ Loader.app/Contents/itms /usr/local/itms
- 如果想要在腳本中修改plist文件的內(nèi)容可以使用
PlistBuddy
,PlistBuddy
是Mac里一個用于命令行下讀寫plist文件的工具杭措,在/usr/libexec/
下∈炙兀可以通過它讀取或修改plist文件的內(nèi)容。
#!/bin/sh
# PlistBuddy程序的絕對路徑
PlistBuddyPath=/usr/libexec/PlistBuddy
appInfoPlistPath="/Volumes/SourceCode/showstart_ios/ShowStart_3.0/Info.plist"
bundleShortVersion=$($PlistBuddyPath -c "print CFBundleShortVersionString" ${appInfoPlistPath})
bundleVersion=$($PlistBuddyPath -c "print CFBundleVersion" ${appInfoPlistPath})
echo "$bundleShortVersion"
echo "$bundleVersion"
buildNumber="3.69"
bundleVersion=$($PlistBuddyPath -c "Set :CFBundleVersion $buildNumber" ${appInfoPlistPath})
bundleVersion=$($PlistBuddyPath -c "print CFBundleVersion" ${appInfoPlistPath})
echo "$bundleVersion"
開始打包
- 百度了一下大多數(shù)文章使用
xcodeuild
和xcrun
編譯導出ipa
xcodebuild -workspace XXX -scheme XXX -configuration Release
xcrun -sdk iphoneos PackageApplication -v "/XXX/XXX.app" -o "/XXX/XXX"
- 另一種是
xcodebuild
的archive
和-exportArchive
,然而最新需要使用--exportOptionsPlist
選項,而不再使用--exportFromat
- 使用
xcodebuild -help
命令可查看--exportOptionsPlist
選項具體描述
如何獲取工程的CODE_SIGN_IDENTITY
和PROVISIONING_PROFILE
- 右鍵
xxx.xcodeproj
顯示包內(nèi)容 然后打開project.pbxproj
文件 -
command F
搜索CODE_SIGN_IDENTITY
PROVISIONING_PROFILE
PROVISIONING_PROFILE
使用uuid
或者名稱都可以注意 因為我這里打包用的
Release
模式所以,我在查找CODE_SIGN_IDENTITY
和PROVISIONING_PROFILE
都是找的Release
模式配置使用
atool
時 最終輸出的xml 中包含success-message
表示成功,如果包含product-errors
表示失敗執(zhí)行腳本
// 首先把兩個plist和腳本放到工程根目錄,然后打開terminal進入項目
// 第一次需要設(shè)置腳本的執(zhí)行權(quán)限
chmod +x xcodebuild.sh
// 執(zhí)行腳本
./xcodebuild.sh
完整的腳本 其中
xxxx
替換為你自己的
#!/bin/sh
echo "~~~~~~~~~~~~~~~~開始執(zhí)行腳本~~~~~~~~~~~~~~~~"
# 開始時間
beginTime=`date +%s`
DATE=`date '+%Y-%m-%d-%T'`
#需要編譯的 targetName
TARGET_NAME="xxxx"
#編譯模式 工程默認有 Debug Release
CONFIGURATION_TARGET=Release
#編譯路徑
BUILDPATH=~/Desktop/${TARGET_NAME}_${DATE}
#archivePath
ARCHIVEPATH=${BUILDPATH}/${TARGET_NAME}.xcarchive
#輸出的ipa目錄
IPAPATH=${BUILDPATH}
#證書名
CODE_SIGN_IDENTITY="xxxxx"
#描述文件
PROVISIONING_PROFILE_NAME="xxxx"
#蘋果賬號
AppleID="xxxx"
AppleIDPWD="xxxx"
#導出ipa 所需plist
ADHOCExportOptionsPlist=./ADHOCExportOptionsPlist.plist
AppStoreExportOptionsPlist=./AppStoreExportOptionsPlist.plist
ExportOptionsPlist=${ADHOCExportOptionsPlist}
# 是否上傳蒲公英
UPLOADPGYER=false
# 是否上傳AppStore
UPLOADAPPSTore=false
echo "~~~~~~~~~~~~~~~~選擇打包方式~~~~~~~~~~~~~~~~"
echo " 1 ad-hoc (默認)"
echo " 2 AppStore "
# 讀取用戶輸入并存到變量里
read parameter
sleep 0.5
method="$parameter"
# 判讀用戶是否有輸入
if [ -n "$method" ]
then
if [ "$method" = "1" ]
then
PROVISIONING_PROFILE_NAME="xxxx"
ExportOptionsPlist=${ADHOCExportOptionsPlist}
elif [ "$method" = "2" ]
then
UPLOADAPPSTore=true
PROVISIONING_PROFILE_NAME="xxxx"
ExportOptionsPlist=${AppStoreExportOptionsPlist}
else
echo "參數(shù)無效...."
exit 1
fi
else
ExportOptionsPlist=${ADHOCExportOptionsPlist}
fi
if [ $UPLOADAPPSTore = false ]
then
echo "~~~~~~~~~~~~~~~~是否上傳蒲公英~~~~~~~~~~~~~~~~"
echo " 1 不上傳 (默認)"
echo " 2 上傳 "
read para
sleep 0.5
if [ -n "$para" ]
then
if [ "$para" = "1" ]
then
UPLOADPGYER=false
elif [ "$para" = "2" ]
then
UPLOADPGYER=true
else
echo "參數(shù)無效...."
exit 1
fi
else
UPLOADPGYER=false
fi
fi
echo "~~~~~~~~~~~~~~~~開始編譯~~~~~~~~~~~~~~~~~~~"
echo "~~~~~~~~~~~~~~~~開始清理~~~~~~~~~~~~~~~~~~~"
# 清理 避免出現(xiàn)一些莫名的錯誤
xcodebuild clean -workspace ${TARGET_NAME}.xcworkspace \
-configuration \
${CONFIGURATION} -alltargets
echo "~~~~~~~~~~~~~~~~開始構(gòu)建~~~~~~~~~~~~~~~~~~~"
#開始構(gòu)建
xcodebuild archive -workspace ${TARGET_NAME}.xcworkspace \
-scheme ${TARGET_NAME} \
-archivePath ${ARCHIVEPATH} \
-configuration ${CONFIGURATION_TARGET} \
CODE_SIGN_IDENTITY="${CODE_SIGN_IDENTITY}" \
PROVISIONING_PROFILE="${PROVISIONING_PROFILE_NAME}"
echo "~~~~~~~~~~~~~~~~檢查是否構(gòu)建成功~~~~~~~~~~~~~~~~~~~"
# xcarchive 實際是一個文件夾不是一個文件所以使用 -d 判斷
if [ -d "$ARCHIVEPATH" ]
then
echo "構(gòu)建成功......"
else
echo "構(gòu)建失敗......"
rm -rf $BUILDPATH
exit 1
fi
endTime=`date +%s`
ArchiveTime="構(gòu)建時間$[ endTime - beginTime ]秒"
echo "~~~~~~~~~~~~~~~~導出ipa~~~~~~~~~~~~~~~~~~~"
beginTime=`date +%s`
xcodebuild -exportArchive \
-archivePath ${ARCHIVEPATH} \
-exportOptionsPlist ${ExportOptionsPlist} \
-exportPath ${IPAPATH}
echo "~~~~~~~~~~~~~~~~檢查是否成功導出ipa~~~~~~~~~~~~~~~~~~~"
IPAPATH=${IPAPATH}/${TARGET_NAME}.ipa
if [ -f "$IPAPATH" ]
then
echo "導出ipa成功......"
else
echo "導出ipa失敗......"
# 結(jié)束時間
endTime=`date +%s`
echo "$ArchiveTime"
echo "導出ipa時間$[ endTime - beginTime ]秒"
exit 1
fi
endTime=`date +%s`
ExportTime="導出ipa時間$[ endTime - beginTime ]秒"
# 上傳AppStore
if [ $UPLOADAPPSTore = true ]
then
altoolPath="/Applications/Xcode.app/Contents/Applications/Application\ Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Versions/A/Support/altool"
${altoolPath} --validate-app \
-f ${IPAPATH} \
-u ${AppleID} \
-p ${AppleIDPWD} \
-t ios --output-format xml
if [ $? = 0 ]
then
echo "~~~~~~~~~~~~~~~~驗證ipa成功~~~~~~~~~~~~~~~~~~~"
${altoolPath} --upload-app \
-f ${IPAPATH} \
-u ${AppleID} \
-p ${AppleIDPWD} \
-t ios --output-format xml
if [ $? = 0 ]
then
echo "~~~~~~~~~~~~~~~~提交AppStore成功~~~~~~~~~~~~~~~~~~~"
else
echo "~~~~~~~~~~~~~~~~提交AppStore失敗~~~~~~~~~~~~~~~~~~~"
fi
else
echo "~~~~~~~~~~~~~~~~驗證ipa失敗~~~~~~~~~~~~~~~~~~~"
fi
else
# 上傳蒲公英
if [ $UPLOADPGYER = true ]
then
echo "~~~~~~~~~~~~~~~~上傳ipa到蒲公英~~~~~~~~~~~~~~~~~~~"
curl -F "file=@$IPAPATH" \
-F "uKey=xxxxx" \
-F "_api_key=xxxx" \
-F "password=xxxxx" \
-F "isPublishToPublic=xxxx" \
https://www.pgyer.com/apiv1/app/upload --verbose
if [ $? = 0 ]
then
echo "~~~~~~~~~~~~~~~~上傳蒲公英成功~~~~~~~~~~~~~~~~~~~"
else
echo "~~~~~~~~~~~~~~~~上傳蒲公英失敗~~~~~~~~~~~~~~~~~~~"
fi
fi
fi
echo "~~~~~~~~~~~~~~~~配置信息~~~~~~~~~~~~~~~~~~~"
echo "開始執(zhí)行腳本時間: ${DATE}"
echo "編譯模式: ${CONFIGURATION_TARGET}"
echo "導出ipa配置: ${ExportOptionsPlist}"
echo "打包文件路徑: ${ARCHIVEPATH}"
echo "導出ipa路徑: ${IPAPATH}"
echo "$ArchiveTime"
echo "$ExportTime"
exit 1