Android Version Code & Name from AppCenter environment variables
Tags: reactnative, flutter, android, devops
Published: 2021-10-11 04:18:59

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/baicvzvm8x5qnsuay1og.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8460p02tpoyl637bqplx.png)

You can [manually change the values](https://stackoverflow.com/a/32138560/11104068) 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"
```

## Let's automate this!

{% github Microsoft/appcenter %}

AppCenter allows you to add [pre-build scripts](https://github.com/Microsoft/appcenter/tree/master/sample-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"
```
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](https://twitter.com/saamerm) if you have any questions. You can support me by following [my small business on LinkedIn](https://www.linkedin.com/company/prototypemakers), to be informed of innovations & products like our free [5-star NumberBomb game on Android](https://play.google.com/store/apps/details?id=com.tfp.numberbomb) & [iOS](https://apps.apple.com/app/numberbomb/id1560372045)!