androidgradleautomation

Android Gradle 'versionCode' not read from environment variable


I have an Android that I deploy to a Google Play alpha track trough a CI server. For the Android versionCode I make use of the CI build number that I inject into the Gradle script through an environment variable.

This used to work fine; but currently Google Play is not accepting any builds. When I manually trigger a alpha upload build (using the gradle-play-publisher plugin) for Gradle I eventually end up with the following error:

APK has an invalid version code.

So when looking (using aapt dump badging apk-path) at the generated APK I see an empty value for the version code (versionCode='').

The relevant code from the build script:

def appVersionCode = System.getenv("BUILD_NUMBER") as Integer ?: 0
defaultConfig {
  ...
  versionCode appVersionCode
  ...
}

It seems the variable is not read correctly; however it reads System.getenv("KEY_PASS") correctly to use for signing.

The variable is also set:

❯ echo $BUILD_NUMBER
1234

Does anyone have an idea why this specific variable doesn't seem to be read (anymore)?

Gradle version: 3.5 with Android Gradle plugin 2.3.1.


Solution

  • Try to change your code to this:

    def appVersionCode = Integer.valueOf(System.env.BUILD_NUMBER ?: 1)
    defaultConfig {
      ...
      versionCode appVersionCode
      ...
    }