How to Update iOS multiple targets version code and version name with environment variables

Divyanshu Negi

🚀🚀 3 min read

We have a React native application, to Automate the deployments we are using Fastlane, to deploy the builds directly to play store and app store the command looks like this

fastlane deploy tag:"1.0.0.33-prod"

This command will break the version name, version code and the environment version for the build into

version_name = 1.0.0
version_code = 33
environment = production

Now the deployment starts where these values are set as environment variables and will be used to build the binary.

Android

In Android its super simple to change the build.gradle file to update the version name and version code based on the environment variables set, like this

def version_code = 1
def version_name = "0.1"

if(System.getenv('ANDROID_VERSION_CODE') || System.getenv('ANDROID_VERSION_NAME')){
	version_code = System.getenv('ANDROID_VERSION_CODE').toInteger()
	version_name = System.getenv('ANDROID_VERSION_NAME')
}

prod {
	dimension 'client'
	applicationId "com.package.name"
	versionCode version_code
	versionName version_name
	minSdkVersion rootProject.ext.minSdkVersion
	targetSdkVersion rootProject.ext.targetSdkVersion
}

As you can see the file looks for Environment variables named ANDROID_VERSION_CODE and ANDROID_VERSION_NAME and sets the versionCode and versionName accordingly.

IOS

iOS is not so easy, as we have multiple targets, if we have only 1 target we can set the values in info.plist like this

<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>

<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>

here if we set the MARKETING_VERSION and CURRENT_PROJECT_VERSION on Environment variables it will be replaced while building, but not so easy with Targets.

Our app has 3 Targets

  • Production
  • Admin
  • Staging

and each can have different version code and version name (for the sake of simplicity I am calling both android and iOS with same names as version code and version names)

So After researching and trying multiple ways I found this solution which worked for me, we can add an extra build script to update our info.plist file while we archive the builds.

here how it looks like

  • open Xcode
  • Go to your project
  • select the target from the list of targets
  • go to Build Phases

  • press on the plus icon

  • select New Run Script Phase

  • Run Script will be added at the bottom

  • Rename it to Whatever you like, I renamed it

  • Drag it to the top with mouse, so this steps happen at the begging of build phase (I am not sure if this is required but to be on the safe side I did)

# Set the build number from environment variable
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${CURRENT_PROJECT_VERSION}" "${PROJECT_DIR}/${INFOPLIST_FILE}"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${CURRENT_PROJECT_VERSION}" "$SRCROOT/appprojectname/appprojectnameStaging-Info.plist"
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString ${MARKETING_VERSION}" "$SRCROOT/appprojectname/appprojectnameStaging-Info.plist"

This is the script we have to add, in case of your project, the only thing you need to update is CURRENT_PROJECT_VERSION and MARKETING_VERSION , this is the environment variable name which you have to set, also check in your info.Plist for this target if it contains CFBundleVersion and CFBundleShortVersionString with these environment variable names.

also check this path

/appprojectname/appprojectnameStaging-Info.plist

this would be different based on your Project name and Target name.

for us the target contains Staging in the end so added that in the path, for you check the proper name and path of your Info.Plist files.

This is something I am writing to keep myself updated in future incase I have to again do this in some other app.

X

Did this post help you ?

I'd appreciate your feedback so I can make my blog posts more helpful. Did this post help you learn something or fix an issue you were having?

Yes

No

X

If you'd like to support this blog by buying me a coffee I'd really appreciate it!

X

Subscribe to my newsletter

Join 107+ other developers and get free, weekly updates and code insights directly to your inbox.

  • No Spam
  • Unsubscribe whenever
  • Email Address

    Powered by Buttondown

    Picture of Divyanshu Negi

    Divyanshu Negi is a VP of Engineering at Zaapi Pte.

    X