-
-
Save carloscuesta/678668da906bb80bdd22c8fd690c4fc4 to your computer and use it in GitHub Desktop.
Increment versionCode task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
} | |
} |
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!
Update: July 2019
">>" is deprecated and removed and the following syntax is favored for versionCode.gradle:
- 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)
}
}
}
- 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)
...
}
- Add
VERSION_CODE=1
to your gradle.properties file. - Build
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
This ends up creating a call like this:
bla bla path.../android/gradlew updateVersion -p ./android -Pversion_number=5.2.8
gradle.properties:
updateVersion.gradle:
build.gradle: