Android + CircleCI + versionCode

Automatically incrementing build numbers in CI is always a hassle. Here's a simple way of using a generated version number in your CI, based on the current time in 10 second intervals. This is based on this post by esplo but with the missing piece of how to read the version in the build.gradle.

Add this to your CircleCI .circleci/config.yml file:

- run:
name: Write versionCode to
command: |
U=$(expr $(date +%s) / 10) # increase by 1 in each 10 seconds
echo "versionCode=${U}" >> app/
cat app/

Add this to the top of your app's build.gradle:

def Properties versionProperties = new Properties()
versionProperties.load(new FileInputStream(file('')))

And then later in your app's build.gradle, to read the versionCode:

versionCode versionProperties['versionCode'].toInteger()

This way, the CircleCI build will write out versionCode to a file, for example:


And then the Gradle build will read it while building the app.