Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Increment versionCode task
task('incrementVersionCode') << {
def versionCode = Integer.parseInt(VERSION_CODE) + 1
ant.propertyfile(file: "../gradle.properties") {
entry(key: "VERSION_CODE", value: versionCode)
}
}
tasks.whenTaskAdded { task ->
if (task.name == 'assembleRelease') {
task.dependsOn 'incrementVersionCode'
}
}
@carloscuesta

This comment has been minimized.

Copy link
Owner Author

carloscuesta commented Sep 3, 2017

  1. Add this file into: ~/rnProject/android/app/versionCode.gradle
  2. Open android/app/build.gradle and apply the following changes:

Apply the versionCode task at the top of the file.

apply from: './versionCode.gradle'

Replace the versionCode line with:

versionCode Integer.parseInt(VERSION_CODE)
@anandprabhu

This comment has been minimized.

Copy link

anandprabhu commented Dec 6, 2017

Adding the above throws me the following error while using react-native run-android

The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.
        at versionCode_28teg2njsm044c6acdk9jjd0h.run(XXXXXX/android/app/versionCode.gradle:1)

FAILURE: Build failed with an exception.
@guyzmo

This comment has been minimized.

Copy link

guyzmo commented Dec 30, 2017

well, simply replace as it states:

task('incrementVersionCode').doLast {
 // …
}

I tried that and the warning is gone, but I haven't checked the release version bump still works yet.

@carloscuesta

This comment has been minimized.

Copy link
Owner Author

carloscuesta commented Apr 29, 2018

Update to this Gist since now I used it in a different way.

The versionCode.gradle remains the same, but now I do not use a whenTaskAdded hook, instead I fire this gradle task through Fastlane

 gradle(task: 'incrementVersionCode', project_dir: 'android/')
task('incrementVersionCode') << {
    def versionCode = Integer.parseInt(VERSION_CODE) + 1
    ant.propertyfile(file: "../gradle.properties") {
        entry(key: "VERSION_CODE", value: versionCode)
    }
}
@k3ithl1m

This comment has been minimized.

Copy link

k3ithl1m commented Nov 9, 2018

Where should I put the VERSION_CODE variable?

@frozenzia

This comment has been minimized.

Copy link

frozenzia commented Nov 16, 2018

Where should I put the VERSION_CODE variable?

You'll want to put that in gradle.properties. (At least that's what I did and got it working - can't claim to be a gradle guru at this point, so perhaps Carlos had some other idea in mind, but my understanding is that any variables in gradle.properties are available for you to use in build.gradle.

I was wanting to both increment my build number AND update my version number. In my case I'm going to pass the new version number in as a parameter, so I've done this:

sample fastlane call: fastlane update_android_versions newVersion:5.2.8 (newVersion passed as parameter)

fastfile:

desc "Update Android version numbers"
lane :update_android_versions do |options|
    # Android update version numbers
    gradle(
        task: "updateVersion",
        project_dir: "./android",
        properties: { "version_number" => options[:newVersion] }
    )
end

This ends up creating a call like this: bla bla path.../android/gradlew updateVersion -p ./android -Pversion_number=5.2.8

gradle.properties:

VERSION_CODE=23  # (gets changed to 24)
VERSION_NAME=3.0.6  # (gets changed to 5.2.8)

updateVersion.gradle:

task('updateVersion') << {
    def versionCode = Integer.parseInt(VERSION_CODE) + 1
    def versionName = version_number
    ant.propertyfile(file: "../gradle.properties") {
        entry(key: "VERSION_CODE", value: versionCode)
        entry(key: "VERSION_NAME", value: versionName)
    }
}

build.gradle:

versionCode Integer.parseInt(VERSION_CODE)
versionName VERSION_NAME
@ko-lem

This comment has been minimized.

Copy link

ko-lem commented Jan 26, 2019

In my case, I just placed the task at the bottom of the build.gradle file.

build.gradle

...

android {
    ...

    defaultConfig {
        ...
        versionCode Integer.parseInt(VERSION_CODE)
        ...
    }

    ...
}

...

task('incrementVersionCode') << {
    def versionCode = Integer.parseInt(VERSION_CODE) + 1
    ant.propertyfile(file: "../gradle.properties") {
        entry(key: "VERSION_CODE", value: versionCode)
    }
}

gradle.properties

...
VERSION_CODE=1 # this will get updated

Fastfile

...

gradle(
  task: "incrementVersionCode",
  project_dir: "android/"
)

git_commit(
  path: "android/gradle.properties",
  message: "Version Bump"
)
...

Thanks all!

@aldrinc

This comment has been minimized.

Copy link

aldrinc commented Jul 14, 2019

Update: July 2019

">>" is deprecated and removed and the following syntax is favored for versionCode.gradle:

  1. Create a new file called versionCode.gradle in android/app with the code below
task('incrementVersionCode') {
    description= "Increments Version Code"
    doLast {
        def versionCode = Integer.parseInt(VERSION_CODE) + 1
        ant.propertyfile(file: "../gradle.properties") {
            entry(key: "VERSION_CODE", value: versionCode)
        }
    }
}

  1. Open android/app/build.gradle and add the following line: apply from: './versionCode.gradle'

Replace the versionCode line under default config in your app's build.gradle with:

    defaultConfig {
        ...
        versionCode 1  # Replace with versionCode Integer.parseInt(VERSION_CODE)
        ...
    }

  1. Add VERSION_CODE=1 to your gradle.properties file.
  2. Build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.