使用 agvtool 管理項目版本號
背景:發(fā)包后當(dāng)版本號相同時,無法判斷包的新舊,另外,蘋果在上傳包時也必須保證同一版本不允許上傳相同build號的 IPA 文件,為此,開發(fā)了動態(tài)更新 ipa 包 build 號功能。
經(jīng)過一系列調(diào)研和測試,目前 iOS 主項目使用的版本號生成策略為兩個:
Xcode Archive:
在項目上使用 shell 工具 PlistBuddy
來修改 info.plist 文件 build 號。
代碼如下
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 中添加一個 shell
Jenkins Archive:
由于 Jenkins 權(quán)限問題,無法正常使用 PlistBuddy
。經(jīng)過調(diào)研和測試,采用 agvtool
來管理版本號。
jenkins 的設(shè)置也非常簡單,首先需要安裝一個插件 Build Timestamp Plugin
,用于產(chǎn)生實時的時間戳,即可使用 ${BUILD_TIMESTAMP}
環(huán)境變量。
如果使用 Xcode plugin,則如下圖配置,如果不是可使用 agvtool command line
配置,參考下文。
如果設(shè)置后 build 號沒有更新,多半是你的項目還不支持 agvtool,下面會講如何適配你的項目。
擴展: agvtool
簡單介紹下,agvtool 是蘋果推出的一款用于管理軟件版本號的工具,可以點擊官方文檔 查看細(xì)節(jié)。使用它可以輕松快捷的對項目 market-version 和 tech-version 進行快速自增和多 target 修改。
agvtool 使用前需要修改一些配置
-
修改 Current Project Version 的值為一個整數(shù),配置 Versioning System 為 Apple Generic
配置1 - 設(shè)置你的 market-version(
CFBundleShortVersionString
) 和 tech-version(CFBundleVersion
)
設(shè)置版本號 - 為避免執(zhí)行 agvtool 時報
Cannot find "$(SRCROOT)/Info.plist
, 需要刪除的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 號)
agvtool next-version -all # 其中 -all 為所有 target
示例:自動加一到更大的整數(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
會自增 build 值到更大的整數(shù),例如 1=>2,1.3 => 2。
- 設(shè)置 tech-version (Build 號) 為指定值
agvtool new-version -all <your_specific_version>
示例:設(shè)置 Build 號為 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 版本號
$ 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 號)
agvtool what-version
示例:查看當(dāng)前 Build 號
$ xcrun agvtool what-version
Current version of project MyProject is:
2.2