Auto-Increment Build Number for Multiple Targets in Xcode

时间秒杀一切 提交于 2019-12-19 07:49:40

问题


I have a "Free" and "Paid" version of my app, and I want to auto-increment both of the build numbers simultaneously, because sometimes I test with the "Free" version and sometimes I test with the "Paid" version depending on what I am doing. These are essentially the same codebase, I just have two targets with a preprocessor directive defined with the "Paid" version to unlock certain things.

I am using the code in this question: Version vs build in XCode

#!/bin/bash    
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

I think I just need to add two more lines specifying the path to the other $INFOPLIST_FILE along the lines of:

 "Print CFBundleVersion" "NEW_PATH/$INFOPLIST_FILE"

and increment it, but how do I get the path to one target's Plist when I am building the other?


回答1:


An update for the latest Xcode, first change "Versioning System" to "Apple Generic" in "Build Settings" tab for all of your targets.

And add this line in "Run Script" of "Build Phases" tab for all of your targets:

agvtool next-version -all

agvtool will do the increment job for you!




回答2:


I figured it out. You need to use the SRCROOT variable. This will give you the base directory for the project. From there, you need to manually specify the location of the info.plist files you wish to use, and run the PlistBuddy -c command with that path.

Here is an example that increments the "Free" version first and then increments the "Paid" version:

#!/bin/bash
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$SRCROOT/Pro-Info.plist")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$SRCROOT/Pro-Info.plist"

You need to make sure you inverse the script for each target, so it uses the $INFOPLIST_FILE variable on the current target and you are specifying the location of the others. You could probably store these in custom variables or specify each one instead of using the $INFOPLIST_FILE variable at all, but they all do essentially the same thing.



来源:https://stackoverflow.com/questions/17911481/auto-increment-build-number-for-multiple-targets-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!