3

I want to increment the User-Defined variable in xcode project. Is there any script to increment User-Defined variables.

I have a user-defined variable "PATCH". And in info.plist, I am assigning this Variable to Bundle version.

I want that this User-Defined Variable will auto-increment when I archive the project using different configurations.

I am attaching the screenshots, hope it will help you to understand my question easily:

Xcode Screenshot:

Build Setting- Xcode

Plist screenshot:

enter image description here

I also go through these Links:

Xcode AutoIncrement Build Version.

StackOverflow Question

But these questions/links did not match my requirement. All I need is to access user-defines variable through script. please help if any one has solution. Any help is appreciated.

Community
  • 1
  • 1
Sahil Mahajan
  • 3,922
  • 2
  • 29
  • 43
  • A word of warning; once you figure out how to set the required values in the `info.plist` ensure you implement the script as a separate target using the *External Build System* template, and make your app target dependent upon this target. You don't want to be messing with `info.plist` as part of the normal build process. There be dragons! – trojanfoe Sep 18 '14 at 11:55
  • I am building my project through jenkins CI and I have different jobs for different configurations. What I need is to increment my variable so that I donot increment build version manually. It will also be done automatically. What are the drawbacks in my above solution. Can u explain me a little bit? – Sahil Mahajan Sep 18 '14 at 12:05
  • You can still use the separate target approach from a command line build, but you might have issues detecting if you are archiving when using a separate target (I cannot remember to be honest). – trojanfoe Sep 18 '14 at 12:09
  • Were you able to figure this out? I'm after the same thing. Thanks! – Jlam Sep 15 '15 at 02:11
  • not yet!! :( I am manually increment the variables. – Sahil Mahajan Sep 15 '15 at 12:25
  • If this is actually a bash problem, and all you want is to increment a number in `info.plist` file, what does the file look like? – miken32 Mar 02 '16 at 21:32
  • Here is the duplicate https://stackoverflow.com/q/38328837/5790492 – Nike Kov Oct 13 '21 at 09:22

3 Answers3

-1

You need to update the file project.pbxproj in *.xcodeproj/.

For example, if you need to update the Debug build number of PATCH. First find the identifier in project.pbxproj for the Debug build, you should get something like 13B07F941A680F5B00A75B9A. Then you can do

/usr/libexec/PlistBuddy -c "print :objects:13B07F941A680F5B00A75B9A:buildSettings:PATCH" project.pbxproj
# which should give 90

Then you can update it with (make a backup copy of project.pbxproj first) :

/usr/libexec/PlistBuddy -c "set :objects:13B07F941A680F5B00A75B9A:buildSettings:PATCH 91" project.pbxproj

Verify again with :

/usr/libexec/PlistBuddy -c "print :objects:13B07F941A680F5B00A75B9A:buildSettings:PATCH" project.pbxproj
# which should give 91

Also check in xcode.

Philippe
  • 20,025
  • 2
  • 23
  • 32
  • Unfortunately PlistBuddy format .pbxproj to xml-format plist so source control getting stack and the information is lost. So I can't use this solution( – Nike Kov Oct 12 '21 at 22:01
  • @NikKov What do you mean by `getting stack` ? – Philippe Oct 12 '21 at 22:10
  • It means that too many changes. Because the project.pbxproj is completely different after `PlistBuddy set`. Check https://stackoverflow.com/a/69519219/5790492 to learn what to do. – Nike Kov Oct 13 '21 at 09:17
-2

Thx @Philippe, i've made a bash script:

# - RELEASE EBF737881ECAECD400F5A5DF
# - DEBUG EBF737871ECAECD400F5A5DF

currentVersion=$(/usr/libexec/PlistBuddy -c "print :objects:EBF737871ECAECD400F5A5DF:buildSettings:FC_VERSION" ../FormaCar.xcodeproj/project.pbxproj)
newVersion="${currentVersion%.*}.$((${currentVersion##*.}+1))"
/usr/libexec/PlistBuddy -c "set :objects:EBF737871ECAECD400F5A5DF:buildSettings:FC_VERSION $newVersion" ../FormaCar.xcodeproj/project.pbxproj
/usr/libexec/PlistBuddy -c "set :objects:EBF737881ECAECD400F5A5DF:buildSettings:FC_VERSION $newVersion" ../FormaCar.xcodeproj/project.pbxproj
echo "Old version: $currentVersion. Version bumped to: $newVersion"

where FC_VERSION is user-defined parameter and holds a version in x.x.x format.

and in Fastfile

lane :bumpMinorVersionNumber do
  sh "./bumpMinorVersionNumber.sh"
end

This script bumps the minor version of FC_VERSION setting and works via fastlane

UPDATE:

Unfortunately PlistBuddy format .pbxproj to xml-format plist so source control getting stack and the information is lost. So I can't use this solution(

Thx to https://stackoverflow.com/a/56179405/5790492 and https://nshipster.com/xcconfig/
I've created the xcconfig file, added it to project in Info tab. For fastlane added this plugin to work with xcconfig. And now lane looks like:

currentBuildNumber = get_xcconfig_value(path: 'fastlane/VersionsConfig.xcconfig',
                                        name: 'FC_BUILD')
newBuildNumber = currentBuildNumber.to_i + 1
update_xcconfig_value(path: 'fastlane/VersionsConfig.xcconfig',
                      name: 'FC_BUILD',
                      value: newBuildNumber.to_s)
UI.important("Old build number: #{currentBuildNumber}. Build number bumped to: #{newBuildNumber}")

It works like a charm!

Nike Kov
  • 12,630
  • 8
  • 75
  • 122
-5

I would suggest you use something like fastlane to accomplish this (there is an action called increment_build_number which does specifically this, more info on the actions page).
From code point of view, if you need to access that variable I use something like:
NSString *buildVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"BUILD_VERSION"];

Also, I noticed that in the plist file you use ${PATCH} ...I think that's supposed to have the same name as the user-defined variable in your project.

tagyro
  • 1,638
  • 2
  • 21
  • 36