iOS自動打包主要用xcodebuild命令, 在終端輸入xcodebuild --help可以查看xcodebuild的參數。
xcodebuild具體語法:
無workspace的工程
xcodebuild [-project name.xcodeproj] [[-target targetname] … |
-alltargets] [-configuration configurationname] [-sdk [sdkfullpath |
sdkname]] [action …] [buildsetting=value …] [-userdefault=value …]
xcodebuild [-project name.xcodeproj] -scheme schemename
[[-destination destinationspecifier] …] [-destination-timeout value]
[-configuration configurationname] [-sdk [sdkfullpath | sdkname]]
[action …] [buildsetting=value …] [-userdefault=value …]
命令中可以添加一些參數實現在命令執行時配置不同的環境。比如,如果想archive出Debug環境的包,那么就可以在命令中添加-configuration Debug參數。還可以通過添加PROVISIONING_PROFILE參數來指定簽名所用的Profile文件的UUID。
對于基于workspace的工程,比如cocoapods項目,腳本格式如下:
xcodebuild -workspace name.xcworkspace -scheme schemename
[[-destination destinationspecifier] …] [-destination-timeout value]
[-configuration configurationname] [-sdk [sdkfullpath | sdkname]]
[action …] [buildsetting=value …] [-userdefault=value …]
當然還有很多可選參數,在這就不一一列舉了,如果有興趣可以通過xcodebuild --help查看。
shell腳本(工程不是WorkSpace)
腳本下載路徑:github下載地址
#author by 得力#注意:腳本目錄和xxxx.xcodeproj要在同一個目錄,如果放到其他目錄,請自行修改腳本。#工程名字(Target名字)Project_Name="Target名字,系統默認和工程名字一樣"#配置環境,Release或者DebugConfiguration="Release"#AdHoc版本的Bundle IDAdHocBundleID="com.xxx"#AppStore版本的Bundle IDAppStoreBundleID="com.xxx"#enterprise的Bundle IDEnterpriseBundleID="com.xxx"# ADHOC#證書名#描述文件ADHOCCODE_SIGN_IDENTITY="iPhone Distribution: xxxx"ADHOCPROVISIONING_PROFILE_NAME="xxxx-xxxx-xxxx-xxxx"#AppStore證書名#描述文件APPSTORECODE_SIGN_IDENTITY="iPhone Distribution: xxxx"APPSTOREROVISIONING_PROFILE_NAME="xxxx-xxxx-xxxx-xxxx"#企業(enterprise)證書名#描述文件ENTERPRISECODE_SIGN_IDENTITY="iPhone Distribution: xxxxx"ENTERPRISEROVISIONING_PROFILE_NAME="xxxx-xxxx-xxxx-xxxx"#加載各個版本的plist文件ADHOCExportOptionsPlist=./ADHOCExportOptionsPlist.plistAppStoreExportOptionsPlist=./AppStoreExportOptionsPlist.plistEnterpriseExportOptionsPlist=./EnterpriseExportOptionsPlist.plistADHOCExportOptionsPlist=${ADHOCExportOptionsPlist}AppStoreExportOptionsPlist=${AppStoreExportOptionsPlist}EnterpriseExportOptionsPlist=${EnterpriseExportOptionsPlist}echo"~~~~~~~~~~~~選擇打包方式(輸入序號)~~~~~~~~~~~~~~~"echo"? 1 appstore"echo"? 2 adhoc"echo"? 3 enterprise"# 讀取用戶輸入并存到變量里readparametersleep 0.5method="$parameter"# 判讀用戶是否有輸入if[ -n"$method"]then#clean下xcodebuild clean -xcodeproj ./$Project_Name/$Project_Name.xcodeproj -configuration$Configuration-alltargetsif["$method"="1"]then#appstore腳本xcodebuild -project$Project_Name.xcodeproj -scheme$Project_Name-configuration$Configuration-archivePath build/$Project_Name-appstore.xcarchive clean archive build? CODE_SIGN_IDENTITY="${APPSTORECODE_SIGN_IDENTITY}"PROVISIONING_PROFILE="${APPSTOREROVISIONING_PROFILE_NAME}"PRODUCT_BUNDLE_IDENTIFIER="${AppStoreBundleID}"xcodebuild -exportArchive -archivePath build/$Project_Name-appstore.xcarchive -exportOptionsPlist$AppStoreExportOptionsPlist-exportPath ~/Desktop/$Project_Name-appstore.ipaelif["$method"="2"]then#adhoc腳本xcodebuild -project$Project_Name.xcodeproj -scheme$Project_Name-configuration$Configuration-archivePath build/$Project_Name-adhoc.xcarchive clean archive build CODE_SIGN_IDENTITY="${ADHOCCODE_SIGN_IDENTITY}"PROVISIONING_PROFILE="${ADHOCPROVISIONING_PROFILE_NAME}"PRODUCT_BUNDLE_IDENTIFIER="${AdHocBundleID}"xcodebuild -exportArchive -archivePath build/$Project_Name-adhoc.xcarchive -exportOptionsPlist$ADHOCExportOptionsPlist-exportPath ~/Desktop/$Project_Name-adhoc.ipaelif["$method"="3"]then#企業打包腳本xcodebuild -project$Project_Name.xcodeproj -scheme$Project_Name-configuration$Configuration-archivePath build/$Project_Name-enterprise.xcarchive clean archive build CODE_SIGN_IDENTITY="${ENTERPRISECODE_SIGN_IDENTITY}"PROVISIONING_PROFILE="${ENTERPRISEROVISIONING_PROFILE_NAME}"PRODUCT_BUNDLE_IDENTIFIER="${EnterpriseBundleID}"xcodebuild -exportArchive -archivePath build/$Project_Name-enterprise.xcarchive -exportOptionsPlist$EnterpriseExportOptionsPlist-exportPath ~/Desktop/$Project_Name-enterprise.ipaelseecho"參數無效...."exit1fifi
注意:1.由于腳本配置的路徑問題,所以xcodebuild.sh和xxx.xcodeproj放到同一個目錄下,否則會出現路徑問題。如圖所示:
腳本目錄和工程在同一個目錄
2.由于Xcode8可以在Project->General中自動配置證書,所以用腳本打包前先去掉該功能。如圖所示:
去掉自動配置證書
3.配置腳本,需要配置的信息如下圖,不需要的版本可以不用配置。比如只需要AppStore的ipa,則只需要配置AppStore版本相關的配置。
配置腳本
4.執行腳本,打開終端,cd到當前腳本所在路徑,然后執行:./xcodebuild.sh即可。
5.導出的ipa包默認保存到桌面,當前保存目錄如果需要修改,可以自己修改腳本。如圖所示,比如我項目名字是OneProject,則導出的包如下圖:
ipa包文件夾
打開文件夾就是ipa包了,如圖:
ipa包
6.如何查看證書名字和配置文件的UUID呢?
打開鑰匙串訪問,找到證書,點擊顯示簡介,里面有個常用名字,復制到腳本中即可,如圖所示:
證書
常用名稱
配置文件UUID可以在Xcode中查看,Xcode->Preferences...->Accounts,如下圖:
點擊View Details
點擊Show in Finder
配置文件UUID
上面的配置文件的名字是6994F55C-1960-4EF9-AA7E-9C1FABDBA7A8.mobileprovision。則配置的文件的UUID就是6994F55C-1960-4EF9-AA7E-9C1FABDBA7A8。所以復制6994F55C-1960-4EF9-AA7E-9C1FABDBA7A8到腳本中。
執行腳本
打開終端,cd到當前腳本所在路徑,在終端輸入:./xcodebuild.sh,點擊回車即可。
WorkSpace腳本
如果你的項目用的workspace,或者cocoapods,則上面的腳本不適用了,具體腳本如下,腳本配置和上面一樣。
腳本下載路徑:github下載地址
#author by 得力#注意:腳本目錄和WorkSpace目錄在同一個目錄#工程名字(Target名字)Project_Name="Target名字,系統默認等于工程名字"#workspace的名字Workspace_Name="WorkSpace名字"#配置環境,Release或者Debug,默認releaseConfiguration="Release"#AdHoc版本的Bundle IDAdHocBundleID="com.xxxx"#AppStore版本的Bundle IDAppStoreBundleID="com.xxxx"#enterprise的Bundle IDEnterpriseBundleID="com.xxxx"# ADHOC證書名#描述文件ADHOCCODE_SIGN_IDENTITY="iPhone Distribution: xxxx"ADHOCPROVISIONING_PROFILE_NAME="xxxxx-xxxx-xxxx-xxxx-xxxxxx"#AppStore證書名#描述文件APPSTORECODE_SIGN_IDENTITY="iPhone Distribution: xxxxx"APPSTOREROVISIONING_PROFILE_NAME="xxxxx-xxxx-xxxx-xxxx-xxxxxx"#企業(enterprise)證書名#描述文件ENTERPRISECODE_SIGN_IDENTITY="iPhone Distribution: xxxx"ENTERPRISEROVISIONING_PROFILE_NAME="xxxxx-xxxx-xxx-xxxx"#加載各個版本的plist文件ADHOCExportOptionsPlist=./ADHOCExportOptionsPlist.plistAppStoreExportOptionsPlist=./AppStoreExportOptionsPlist.plistEnterpriseExportOptionsPlist=./EnterpriseExportOptionsPlist.plistADHOCExportOptionsPlist=${ADHOCExportOptionsPlist}AppStoreExportOptionsPlist=${AppStoreExportOptionsPlist}EnterpriseExportOptionsPlist=${EnterpriseExportOptionsPlist}echo"~~~~~~~~~~~~選擇打包方式(輸入序號)~~~~~~~~~~~~~~~"echo"? 1 adHoc"echo"? 2 AppStore"echo"? 3 Enterprise"# 讀取用戶輸入并存到變量里readparametersleep 0.5method="$parameter"# 判讀用戶是否有輸入if[ -n"$method"]thenif["$method"="1"]then#adhoc腳本xcodebuild -workspace$Workspace_Name.xcworkspace -scheme$Project_Name-configuration$Configuration-archivePath build/$Project_Name-adhoc.xcarchive clean archive build CODE_SIGN_IDENTITY="${ADHOCCODE_SIGN_IDENTITY}"PROVISIONING_PROFILE="${ADHOCPROVISIONING_PROFILE_NAME}"PRODUCT_BUNDLE_IDENTIFIER="${AdHocBundleID}"xcodebuild? -exportArchive -archivePath build/$Project_Name-adhoc.xcarchive -exportOptionsPlist${ADHOCExportOptionsPlist}-exportPath ~/Desktop/$Project_Name-adhoc.ipaelif["$method"="2"]then#appstore腳本xcodebuild -workspace$Workspace_Name.xcworkspace -scheme$Project_Name-configuration$Configuration-archivePath build/$Project_Name-appstore.xcarchive archive build CODE_SIGN_IDENTITY="${APPSTORECODE_SIGN_IDENTITY}"PROVISIONING_PROFILE="${APPSTOREROVISIONING_PROFILE_NAME}"PRODUCT_BUNDLE_IDENTIFIER="${AppStoreBundleID}"xcodebuild? -exportArchive -archivePath build/$Project_Name-appstore.xcarchive -exportOptionsPlist${AppStoreExportOptionsPlist}-exportPath ~/Desktop/$Project_Name-appstore.ipaelif["$method"="3"]then#企業打包腳本xcodebuild -workspace$Workspace_Name.xcworkspace -scheme$Project_Name-configuration$Configuration-archivePath build/$Project_Name-enterprise.xcarchive archive build CODE_SIGN_IDENTITY="${ENTERPRISECODE_SIGN_IDENTITY}"PROVISIONING_PROFILE="${ENTERPRISEROVISIONING_PROFILE_NAME}"PRODUCT_BUNDLE_IDENTIFIER="${EnterpriseBundleID}"xcodebuild? -exportArchive -archivePath build/$Project_Name-enterprise.xcarchive -exportOptionsPlist${EnterpriseExportOptionsPlist}-exportPath ~/Desktop/$Project_Name-enterprise.ipaelseecho"參數無效...."exit1fifi
可能出現的問題
1.如果配置的證書名字、BundleID、配置文件UUID不一致,腳本就會報錯,這個可以看腳本提示錯誤,由于錯誤非常明顯,所以就不在截圖了。
2.如果執行腳本的過程出現如下錯誤,在終端輸入:rvm system,回車即可。
3.如果執行腳本的過程出現-bash: ./xx.sh: Permission denied,表示權限問題,所以在終端執行:chmod 777 xx.sh,然后回車即可,但是要注意:chmod 777后面是腳本路徑。