One very useful method of calculating the code version is to use Codemagic command line tools to get the latest build number from Google Play and increment it by one.
You can find the full sample project with the instructions on alternative ways to perform Android build versioning in our repository.
The prerequisite is a valid Google Cloud Service Account. Please follow these steps:
- Go to this guide and complete the steps in the Google Play section.
- Skip to the Creating a service account section in the same guide and complete those steps also.
- You now have a
JSON
file with the credentials. - Open Codemagic UI and create a new Environment variable
GOOGLE_PLAY_SERVICE_ACCOUNT_CREDENTIALS
. - Paste the content of the downloaded
JSON
file in the Value field, set the group name (e.g. google_play) and make sure the Secret option is checked.
- Add the google_play variable group to the
codemagic.yaml
workflows: android-workflow-id: # .... environment: groups: - google_play
- Modify the build script to calculate the build number and use it as gradlew arguments.
scripts: # .... - name: Build Android release script: | LATEST_GOOGLE_PLAY_BUILD_NUMBER=$(google-play get-latest-build-number --package-name "$PACKAGE_NAME") if [ -z $LATEST_GOOGLE_PLAY_BUILD_NUMBER ]; then # fallback in case no build number was found from Google Play. # Alternatively, you can `exit 1` to fail the build # BUILD_NUMBER is a Codemagic built-in variable tracking the number # of times this workflow has been built UPDATED_BUILD_NUMBER=$BUILD_NUMBER else UPDATED_BUILD_NUMBER=$(($LATEST_GOOGLE_PLAY_BUILD_NUMBER + 1)) fi cd android ./gradlew bundleRelease \ -PversionCode=$UPDATED_BUILD_NUMBER \ -PversionName=1.0.$UPDATED_BUILD_NUMBER
- Modify the
android/app/build.gradle
file to get the build number values and apply them:// get version code from the specified property argument `-PversionCode` during the build call def getMyVersionCode = { -> return project.hasProperty('versionCode') ? versionCode.toInteger() : -1 } // get version name from the specified property argument `-PversionName` during the build call def getMyVersionName = { -> return project.hasProperty('versionName') ? versionName : "1.0" } .... android { .... defaultConfig { ... versionCode getMyVersionCode() versionName getMyVersionName()