Xcode 13移除Info.plist后,如何通过Shell修改iOS自定义Target的Bundle版本?
如何通过Shell修改iOS自定义Target的CFBundleVersion(替代Info.plist方式)
随着Xcode的更新,原有的Info.plist文件已被整合到项目的project.pbxproj配置中,无法再通过PlistBuddy直接修改CFBundleVersion。以下是两种适合持续部署(CD)脚本的解决方案:
方法一:使用xcodebuild命令直接设置(推荐)
xcodebuild支持在命令行直接指定项目构建参数,其中CURRENT_PROJECT_VERSION对应CFBundleVersion的配置项。
针对xcodeproj项目:
xcodebuild -project YourProject.xcodeproj -target YourCustomTarget -configuration Release CURRENT_PROJECT_VERSION=45
针对xcworkspace项目:
xcodebuild -workspace YourWorkspace.xcworkspace -scheme YourScheme -configuration Release CURRENT_PROJECT_VERSION=45
该命令会自动将版本号同步到项目配置中,无需手动修改文件,稳定性更高。
方法二:直接修改project.pbxproj文件
如果需要更精细的控制,可以通过sed命令直接替换配置文件中的版本号:
# 替换所有Target的CURRENT_PROJECT_VERSION值为45 sed -i '' 's/CURRENT_PROJECT_VERSION = [0-9]*;/CURRENT_PROJECT_VERSION = 45;/g' YourProject.xcodeproj/project.pbxproj
若需仅修改指定Target的版本号,可结合Target名称精准匹配(假设Target名称为"MyCustomTarget"):
sed -i '' '/MyCustomTarget/,/CURRENT_PROJECT_VERSION/s/CURRENT_PROJECT_VERSION = [0-9]*;/CURRENT_PROJECT_VERSION = 45;/' YourProject.xcodeproj/project.pbxproj
自动递增版本号示例
如果需要在CD脚本中实现版本号自动递增,可先获取当前版本号再计算新版本:
# 获取指定Target的当前版本号 current_version=$(xcodebuild -project YourProject.xcodeproj -target YourCustomTarget -configuration Release -showBuildSettings | grep CURRENT_PROJECT_VERSION | awk '{print $3}') # 计算递增后的版本号 new_version=$((current_version + 1)) # 设置新版本号 xcodebuild -project YourProject.xcodeproj -target YourCustomTarget -configuration Release CURRENT_PROJECT_VERSION=$new_version
内容的提问来源于stack exchange,提问作者NSDo
相关产品推荐
相关产品推荐

