1. 安裝多個版本的Xcode
因為業(yè)務(wù)需要,我們有時候需要安裝多個xcode版本,新的版本,老的版本。新的版本我們以xcode11為例子,老的版本,我們以xcode10為例子,
下載老版本到: https://developer.apple.com/download/more/ 下載xcode10版本。
1.下載以后 我們看到是一個xcode10.xip文件,這是我第一次看到xip后綴的文件, 原來雙擊一下就可以了,會解壓出一個xcode.app 的文件夾。
2.在/Applications目錄下新建一個文件夾Xcode10,將剛才第一步中得到的xcode.app移動到/Applications/Xcode10/目錄下
根據(jù)Mac系統(tǒng)版本,下載安裝兼容的Xcode版本。注意,需要安裝對應(yīng)版本的Command Line Tools
。
Xcode下載鏈接
2.管理多個Xcode版本
2.1 查看當(dāng)前Xcode版本
2.1.1 打印當(dāng)前活躍的Xcode文件夾路徑
xcode-select -p
2.1.2 打印Xcode版本號
xcodebuild -version
2.1.3 從gcc編譯器的版本信息中找
gcc --version
2.2 切換版本
sudo xcode-select -s /Applications/Xcode10/Xcode.app/Contents/Developer
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
主要通過終端的xcode-select命令操作:
Usage: xcode-select [options]
Print or change the path to the active developer directory. This directory controls which tools are used for the Xcode command line tools (for example, xcodebuild) as well as the BSD development commands (such as cc and make).
Options:
-h, --help print this help message and exit
-p, --print-path print the path of the active developer directory
-s <path>, --switch <path> set the path for the active developer directory
--install open a dialog for installation of the command line developer tools
-v, --version print the xcode-select version
-r, --reset reset to the default command line tools path
sudo xcode-select --switch <xcode_folder_path>
sudo模式需要驗證用戶密碼
確保傳入了正確的Xcode路徑,例如/Applications/Xcode/15.0_beta3/Xcode.app/Contents/Developer,否則會報路徑錯誤error: invalid developer directory '...'
2.3 jenkins自動打包時切換Xcode版本
通過腳本自動檢測、切換打包用的Xcode版本:
change_xcode_version()
{
[ "$1" != "11" -a "$1" != "13" ] && echo "input is $1,not 11 or 13" && exit 1
xcode_v=$1
echo "xcode version:"
xcodebuild -version
[ "$1" == "11" ] && export DEVELOPER_DIR=/Applications/Xcode/11.4/Xcode.app/Contents/Developer
[ "$1" == "13" ] && export DEVELOPER_DIR=/Applications/Xcode/13.1/Xcode.app/Contents/Developer
./expect_xcode $1 # 調(diào)用切換Xcode版本的腳本
sleep 10
xcodebuild -version
# 檢查DEVELOPER_DIR是否切換成功,否則異常退出
xcode_ver=`xcodebuild -version |grep Xcode |awk -F " " '{print $2}'`
[ "$1" == "11" ] && [ "$xcode_ver" != "11.4" ] && echo "xcode version $xcode_ver, not 11.4" && exit 1
[ "$1" == "13" ] && [ "$xcode_ver" != "13.1" ] && echo "xcode version $xcode_ver, not 13.1" && exit 1
echo "new xcode version:"
xcodebuild -version
}
其中,切換Xcode版本的腳本
#!/usr/bin/expect
set timeout 10
set version [lindex $argv 0]
set password "123456" # 開機密碼
if {$version == "13" } {
spawn sudo xcode-select -s /Applications/Xcode/13.1/Xcode.app/Contents/Developer
}
if {$version == "11" } {
spawn sudo xcode-select -s /Applications/Xcode/11.4/Xcode.app/Contents/Developer
}
expect "*assword*" {send "$password\r"} # 自動輸入密碼
interact