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 version.properties
command: |
U=$(expr $(date +%s) / 10) # increase by 1 in each 10 seconds
echo "versionCode=${U}" >> app/version.properties
cat app/version.properties

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

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

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 version.properties file, for example:

versionCode=154435338

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