1. 安装多个版本的Xcode
一般首个Xcode直接到AppStore安装了,更多版本的Xcode可以到开发者后台下载安装。
根据Mac系统版本,下载安装兼容的Xcode版本。注意,需要安装对应版本的Command Line Tools。
2. 管理多个Xcode版本
主要通过终端的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
2.1 查看当前Xcode版本
有多种方法:
2.1.1 打印当前活跃的Xcode文件夹路径
终端输入命令
xcode-select -p
输出结果
2.1.2 打印Xcode版本号
终端输入命令
xcode --version
输出结果
2.1.3 从gcc编译器的版本信息中找
终端输入命令
gcc --version
输出结果
2.2 切换Xcode版本
通过xcode-select命令切换当前Xcode版本:
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 # 调用切换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