DEV Community

Cover image for Android Version Code & Name from AppCenter environment variables
Saamer Mansoor
Saamer Mansoor

Posted on

Android Version Code & Name from AppCenter environment variables

Android developers using ReactNative, Flutter, Java or Kotlin use Microsoft AppCenter, a free, neat and extremely popular DevOps tool, to automate app build creation and distribution. A popular challenge is the ability to set your App's Version Name and Build Number/Code without making constant code changes, so that you can easily create newer apps with the click of the Build button in AppCenter.

What is the Version Code and Name?

The Version Name is the value that you see in the Play Store as "Current Version" shown below. The version code (or build number) is not visible to the end-user, but just like the version name you cannot upload an app that has a version code less than or equal to the existing one.
Play Store screenshots

Build Settings changes:

If you haven't started using AppCenter, you just need to create a new Android app, select the App language and then connect your Git Repository, then against a specific branch you can select specific build settings so it automatically creates a new build with every Pull Request merged, for your QA to test.
AppCenter build settings changes

You can manually change the values of the versionCode and versionName in your build.gradle, but that can't be independently done by someone non-technical, and requires another manual code change & pull request by a developer, approval by reviewer of code like this:

versionCode 201
versionName "1.2.1"
Enter fullscreen mode Exit fullscreen mode

Let's automate this!

GitHub logo microsoft / appcenter

Central repository for App Center open source resources and planning.

Overview

Welcome to the primary repository for Visual Studio App Center. Here you'll find a list of active open source repositories our team contributes to, monthly iteration plans with key changes we plan to make and issues containing active discussions around new features and changes to the service.

Have an issue or suggestion for the App Center team? Let us know!

Contents

Planning

  1. Planning Process
  2. Roadmap
  3. Iteration Plans

Over the past few years our team’s focus has been to deliver a first class DevOps experience for mobile and desktop developers. A substantial part of this journey involved building the next generation of HockeyApp services for our customers. Earlier this year, we completed the HockeyApp shut down. We are now looking at what's next for the App Center service.

Just like any software engineering team, as we focused on building and…

AppCenter allows you to add pre-build scripts that can provide a lot more flexibility, but this feature is very easy to automate, and doesn't even need a shell script! To understand the changes, it's important to note that Android expects an integer value for Version Code and a string value for Version Name! When you put any value into the AppCenter environment variable dictionary, you can retrieve the values using the System.getenv() function, so we simply replace our build.gradle code from above, with this:

versionCode System.getenv("VERSION_CODE") ? System.getenv("VERSION_CODE").toInteger() : 201
versionName System.getenv("VERSION_NAME") ? System.getenv("VERSION_NAME").toString() : "1.2.1"
Enter fullscreen mode Exit fullscreen mode

We added the ternary null checker so that the code works if you try to manually deploy from your local machine as well. That's it, just build and give it a test!


Please subscribe so I know people out there want me to write more technical articles in my free time, and feel free to let me know on Twitter if you have any questions. You can support me by following my small business on LinkedIn, to be informed of innovations & products like our free 5-star NumberBomb game on Android & iOS!

Oldest comments (0)