Is it possible to set android version code and name via a Gradle task?
You can create methods to update the versionCode and versionName from the command line:
def getMyVersionCode = { -> def code = project.hasProperty('versionCode') ? versionCode.toInteger() : -1 println "VersionCode is set to $code" return code}def getMyVersionName = { -> def name = project.hasProperty('versionName') ? versionName : "1.0" println "VersionName is set to $name" return name}
Then in the android block:
defaultConfig { applicationId "your.app.id" minSdkVersion 15 targetSdkVersion 23 versionCode getMyVersionCode() versionName getMyVersionName() archivesBaseName = "YourApp-${android.defaultConfig.versionName}" }
Then you can just call any task really:
./gradlew assembleDebug -PversionCode=483 -PversionName=4.0.3
Read more about it here: https://web.archive.org/web/20160119183929/https://robertomurray.co.uk/blog/2013/gradle-android-inject-version-code-from-command-line-parameter/
I found a clean way that is compatible with CI tools (without edit your code):
./gradlew assembleDebug -Pandroid.injected.version.code=1234 -Pandroid.injected.version.name=1.2.3.4
there are more params here for other things like signing:
For GitLab CI:
I use this command for a debug version:
./gradlew assembleDebug -Pandroid.injected.version.code=$CI_PIPELINE_IID -Pandroid.injected.version.name=$CI_COMMIT_SHORT_SHA
You can echo $CI_PIPELINE_IID
if you want to know its number. It increases every time you run a new pipeline for your project.
And for a release version:
./gradlew assembleRelease -Pandroid.injected.signing.store.file=$SIGNING_STORE_FILE -Pandroid.injected.signing.store.password=$SIGNING_STORE_PASSWORD -Pandroid.injected.signing.key.alias=$SIGNING_KEY_ALIAS -Pandroid.injected.signing.key.password=$SIGNING_KEY_PASSWORD -Pandroid.injected.signing.v1-enabled=true -Pandroid.injected.signing.v2-enabled=true -Pandroid.injected.version.code=$CI_PIPELINE_IID -Pandroid.injected.version.name=$CI_COMMIT_TAG
Note: first 4 env vars are set in my project variables and are not internal gitlab ci variables!