Bump version code for Android apps using GitHub Actions
Tags: android
Published: 2023-04-17 10:41:03

---
title: Bump version code for Android apps using GitHub Actions
published: true
date: 2023-04-17 10:39:32 UTC
tags: Android
canonical_url: https://www.rockandnull.com/github-actions-android-version/
---

 ![Bump version code for Android apps using GitHub Actions](https://images.unsplash.com/photo-1465343161283-c1959138ddaa?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDJ8fGluY3JlYXNlfGVufDB8fHx8MTY4MTcyNzkyNQ&ixlib=rb-4.0.3&q=80&w=2000)

Play Store, since the beginning of time, had the same policy about [versioning](https://developer.android.com/studio/publish/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](https://dev.to/rockandnull/android-cicd-part-1-locally-building-and-pushing-to-play-store-using-fastlane-57np) 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 "github-actions@github.com"
          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/create-pull-request@v5.0.0
        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!