使用 agvtool 管理項(xiàng)目版本號(hào)
背景:發(fā)包后當(dāng)版本號(hào)相同時(shí),無(wú)法判斷包的新舊,另外,蘋(píng)果在上傳包時(shí)也必須保證同一版本不允許上傳相同build號(hào)的 IPA 文件,為此,開(kāi)發(fā)了動(dòng)態(tài)更新 ipa 包 build 號(hào)功能。
經(jīng)過(guò)一系列調(diào)研和測(cè)試,目前 iOS 主項(xiàng)目使用的版本號(hào)生成策略為兩個(gè):
Xcode Archive:
在項(xiàng)目上使用 shell 工具 PlistBuddy 來(lái)修改 info.plist 文件 build 號(hào)。
代碼如下
echo $CONFIGURATION
if [ "Release" == "${CONFIGURATION}" ]
then
buildNumber="`date +%y%m%d%H%M%S`"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "你的plist路徑"
echo "build number increase"
fi
在 Xcode -> target -> Build Phase 中添加一個(gè) shell


Jenkins Archive:
由于 Jenkins 權(quán)限問(wèn)題,無(wú)法正常使用 PlistBuddy。經(jīng)過(guò)調(diào)研和測(cè)試,采用 agvtool 來(lái)管理版本號(hào)。
jenkins 的設(shè)置也非常簡(jiǎn)單,首先需要安裝一個(gè)插件 Build Timestamp Plugin
,用于產(chǎn)生實(shí)時(shí)的時(shí)間戳,即可使用 ${BUILD_TIMESTAMP}環(huán)境變量。
如果使用 Xcode plugin,則如下圖配置,如果不是可使用 agvtool command line 配置,參考下文。

如果設(shè)置后 build 號(hào)沒(méi)有更新,多半是你的項(xiàng)目還不支持 agvtool,下面會(huì)講如何適配你的項(xiàng)目。
擴(kuò)展: agvtool
簡(jiǎn)單介紹下,agvtool 是蘋(píng)果推出的一款用于管理軟件版本號(hào)的工具,可以點(diǎn)擊官方文檔 查看細(xì)節(jié)。使用它可以輕松快捷的對(duì)項(xiàng)目 market-version 和 tech-version 進(jìn)行快速自增和多 target 修改。


agvtool 使用前需要修改一些配置
-
修改 Current Project Version 的值為一個(gè)整數(shù),配置 Versioning System 為 Apple Generic
配置1 - 設(shè)置你的 market-version(
CFBundleShortVersionString) 和 tech-version(CFBundleVersion)
設(shè)置版本號(hào) - 為避免執(zhí)行 agvtool 時(shí)報(bào)
Cannot find "$(SRCROOT)/Info.plist, 需要?jiǎng)h除的plist路徑中的$(SRCROOT),如下圖。
移除$(SRCROOT)
agvtool command line的一些基本功能
- 更新 market-version 為指定的版本
agvtool new-marketing-version <your_specific_version>
示例:更新 market-version 為 2.0
$ xcrun agvtool new-marketing-version 2.0
Setting CFBundleShortVersionString of project MyProject to:
2.0.
Updating CFBundleShortVersionString in Info.plist(s)...
Updated CFBundleShortVersionString in "MyProject.xcodeproj/../MyProject/MyProject-Info.plist" to 2.0
- 更新 tech-version (Build 號(hào))
agvtool next-version -all # 其中 -all 為所有 target
示例:自動(dòng)加一到更大的整數(shù)
$ xcrun agvtool next-version -all
Setting version of project MyProject to:
2.
Also setting CFBundleVersion key (assuming it exists)
Updating CFBundleVersion in Info.plist(s)...
注意:agvtool next-version -all 會(huì)自增 build 值到更大的整數(shù),例如 1=>2,1.3 => 2。
- 設(shè)置 tech-version (Build 號(hào)) 為指定值
agvtool new-version -all <your_specific_version>
示例:設(shè)置 Build 號(hào)為 2.6.9
$ xcrun agvtool new-version -all 2.6.9
Setting version of project MyProject to:
2.6.9
Also setting CFBundleVersion key (assuming it exists)
Updating CFBundleVersion in Info.plist(s)...
Updated CFBundleVersion in "MyProject.xcodeproj/../MyProject/MyProject-Info.plist" to 2.6.9
- 查看當(dāng)前 market-version
agvtool what-marketing-version
示例:查看當(dāng)前 market-version 版本號(hào)
$ xcrun agvtool what-marketing-version
No marketing version number (CFBundleShortVersionString) found for Jambase targets.
Looking for marketing version in native targets...
Looking for marketing version (CFBundleShortVersionString) in native targets...
Found CFBundleShortVersionString of "2.0" in "MyProject.xcodeproj/../MyProject/MyProject-Info.plist"
- 查看當(dāng)前 tech-version(Build 號(hào))
agvtool what-version
示例:查看當(dāng)前 Build 號(hào)
$ xcrun agvtool what-version
Current version of project MyProject is:
2.2


