Freelancing for Pale Blue

Looking for flexible work opportunities that fit your schedule?


Bump version code for Android apps using GitHub Actions

Android Apr 17, 2023

Play Store, since the beginning of time, had the same policy about versioning: every artifact uploaded and served from Play Store should have a unique version code. And this is a reasonable requirement for tracking reasons.

The only downside is that they leave the developer the responsibility of bumping and maintaining the version code. There's no way to "auto-increase" the version code. The version code is stored in the build.gradle file which means the codebase needs to be touched for the version code to be kept in sync with what you are uploading to the store.

To automate the process of bumping the version code and then committing the updated version to the codebase, we can have a GitHub Action (if you use GitHub; but the equivalent can be created for other sites) that does the "boring" work for us. More specifically, the process can be as follow:

  1. Create a branch called bump/vX.Y.Z and push to remote.
  2. The GitHub Action gets triggered and will create a PR that (1) sets the version name to X.Y.Z and (2) increases the version code by 1.
  3. You can then review the PR to ensure that all the changes are correct, and proceed with the new release (see this post for automating the push-to-store process in GitHub).
name: Bump version code & change version name
on:
  push:
    branches:
      - 'bump/v*.*.*'
      
jobs:
  bump-version-and-open-pr:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        
      - name: Extract existing version code
        run: |
          # Extract version number from branch name
          version_name=${GITHUB_REF#refs/heads/bump/v}

          # Get existing version code from build.gradle
          version_code=$(grep "versionCode" app/build.gradle | awk '{print $2}' | tr -d '\n')

          # Increment existing version code by 1
          version_code=$((version_code + 1))

          # Set environment variable for later use
          echo "VERSION_NAME=$version_name" >> $GITHUB_ENV
          echo "VERSION_CODE=$version_code" >> $GITHUB_ENV

      - name: Increase version code and change version name
        run: |
          # Update build.gradle with new version code and name
          echo "${{ env.VERSION_CODE }} - ${{ env.version_name }}"
          sed -i "s/versionCode [0-9]\+/versionCode ${{ env.VERSION_CODE }}/g" app/build.gradle
          sed -i "s/versionName \"[^\"]*\"/versionName \"${{ env.VERSION_NAME }}\"/g" app/build.gradle

      - name: Commit and push changes
        run: |
          git config user.email "[email protected]"
          git config user.name "Github Actions"
          git commit -am "Bump version code and change version name"
          git push origin HEAD
          
      - name: Open pull request
        uses: peter-evans/[email protected]
        with:
          commit-message: "Bumping to ${{ env.VERSION_CODE }}"
          branch: "${{ github.ref }}"
          title: "${{ env.VERSION_NAME }} - (${{ env.VERSION_CODE }})"
          body: "${{ env.VERSION_NAME }} - (${{ env.VERSION_CODE }})"
          base: "master"
/.github/workflows/bump.yml

Adjust the above script to your needs, and you have a semi-automated way of bumping version code before releasing without touching Android Studio.

Happy coding!

Tags

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.