Trying to use $BITRISE_BUILD_NUMBER in my gradle but kept running into build fail

Hello all!

I’m trying to use $BITRISE_BUILD_NUMBER in my gradle file as the versionCode, but I kept running into build error claiming that it can’t find versionCode method in gradle…

So what I did is:

versionName System.getenv(“BITRISE_BUILD_NUMBER”)

in my build.gradle file.

Anyone has an idea on why and how should I fix this? Thanks!

1 Like

Can you please copy paste the relevant error line(s)?

BTW “it can’t find versionCode method in gradle” - the snippet you inserted sets the versionName, not the versionCode

My bad! This is the exact line:

versionCode System.getenv(“BITRISE_BUILD_NUMBER”)

And here is the error I got:

What went wrong:
A problem occurred evaluating project ‘:app’.
Could not find method versionCode() for arguments [143] on ProductFlavor_Decorated{name=main, dimension=null, minSdkVersion=null, targetSdkVersion=null, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode=null, versionName=null, applicationId=fr.xebia.fashicon, testApplicationId=null, testInstrumentationRunner=null, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}, mWearAppUnbundled=null} of type

I saw someone does like this.

Am I suppose to be able to use the ENV variable directly in my gradle file? Or do I need to wrap the Bitrise ENV variables in my App variables and then pass it via command line while executing a gradle task?

You can use ENV variables directly in Gradle.

Do you try to run this locally? Because BITRISE_BUILD_NUMBER is only set on automatically, locally you’ll have to set this manually as an environment variable, e.g. in your .bitrise.secrets.yml, or just with an export BITRISE_BUILD_NUMBER=X

To define a default value when BITRISE_BUILD_NUMBER is not available, you can do something like this: Intermittent build failures to execute push_to_git_remote & how to manage build number

def bitriseBuildNumber = System.getenv("BITRISE_BUILD_NUMBER")
def buildNumber = bitriseBuildNumber != null ? bitriseBuildNumber : "0"
1 Like

Yeah I have a mock BITRISE_BUILD_NUMBER in my bash_profile to ensure that runs locally. Locally it works well.

I’ll try other solutions. Thanks for the help :slight_smile:

1 Like

Any time :wink:

Btw if it works locally with the Bitrise CLI, then you should check this guide: How to debug your build locally / "It works on my Mac/PC but not on"

Mainly, you should do a clean git clone of the repo, as there can be a file in your current directory which is not under version control, and so is not available when clones the repo.

Old topic, but still : If the APK versionCode is set with this buildNumber value, it must first be parsed to integer. Also, in my experience, the versionCode can not be set to 0, else the generated APK will have an empty value for versionCode (tried with aapt dump badging APK.apk).
Personally, I prefer to set an arbitrary high value when built in local. Altogether, here is what works both on Bitrise and in local for me :

def bitriseBuildNumber = System.getenv("BITRISE_BUILD_NUMBER")
def buildNumber = bitriseBuildNumber != null ? bitriseBuildNumber : "999999999"
versionCode = buildNumber.toInteger()
1 Like

Definitely a useful note, thank you @raphaelmina! :slight_smile: