Skip to content

Instantly share code, notes, and snippets.

@carloscuesta
Created September 3, 2017 08:58
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save carloscuesta/678668da906bb80bdd22c8fd690c4fc4 to your computer and use it in GitHub Desktop.
Save carloscuesta/678668da906bb80bdd22c8fd690c4fc4 to your computer and use it in GitHub Desktop.
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'
}
}
@ko-lem
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
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