Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 945
Dries Avatar asked May 17 '17 17:05

Dries


People also ask

How do I pass environment variables in gradle task?

If you are using an IDE, go to run, edit configurations, gradle, select gradle task and update the environment variables.

What is versionCode and versionName in Android?

versionCode — A positive integer used as an internal version number. This number is used only to determine whether one version is more recent than another, with higher numbers indicating more recent versions. This is not the version number shown to users; that number is set by the versionName setting, below.

Do we need to set environment variables for Android studio?

You can set environment variables for Android Studio and the command-line tools that specify things like where the SDK is installed and where user-specific data is stored.


1 Answers

Try to change your code to this:

def appVersionCode = Integer.valueOf(System.env.BUILD_NUMBER ?: 0)
defaultConfig {
  ...
  versionCode appVersionCode
  ...
}
like image 69
Luiz Fernando Salvaterra Avatar answered Sep 18 '22 14:09

Luiz Fernando Salvaterra