Is it possible to set android version code and name via a Gradle task?
Asked Answered
S

2

10

I'm trying to automate build process on CI that I'm working with. I am able to call a curl and assign it some variables such as version code and names. Then CI (in my case Bitrise CI) catch it and starts Release build. However, before that I want to set version code and version name based on what has been passed by curl into build.gradle file and then build process starts.

So, I'm thinking I can write a plugin/task that gets version code/name from a command line and then inject it in build.gradle file. A command like ./gradlew setVersion 1 1.0.

Threefore, by running this command from an script that I'll write, I will be able to run this gradle task and everyone from anywhere in the glob is able to create a release build by curl. Quite interesting :)

I am able to write a task similar to following code an put it into my main build.gradle file.

task setVersion << {
    println versionCode
    println versionName
}

and pass it some parameters via command line:

./gradlew -PversionCode=483 -PversionName=v4.0.3 setVersion

This is my output:

:setVersion
483
v4.0.3

BUILD SUCCESSFUL

Total time: 6.346 secs

So far so good. My question is how to set it in build.gradle file?

Sweettalk answered 25/3, 2016 at 4:24 Comment(1)
As I commented on @brwngrldev's answer, I wrote a post regard how Automate Android Build system. Please have a look at this article if you are interested. medium.com/@hesam.kamalan/…Sweettalk
L
23

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/

Lublin answered 25/3, 2016 at 14:31 Comment(4)
thanks for your answer. The only problem it has - in my opinion :) - that I have to pass version code and name for every gradle tasks, particularly assembleDebug. I believe other developers complain :) I prefer to have version code and name untouched but those methods inject version code and name into default Config block. Is it still possible? ThanksSweettalk
You can try this technique, it's a little more complicated than what you wanted though: jayway.com/2015/03/11/…Lublin
I implemented my automation based on #brwngrldev suggestion. I read that article but didn't find it useful for my case. Advantage of her suggestion is the fact that I'm able to put the command in script and run it easily. I'll document it for other programmers later. Thanks again.Sweettalk
Much simpler with default value versionCode project.hasProperty('versionCode') ? project['versionCode'].toInteger() : 124Susi
M
5

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:

https://www.javadoc.io/static/com.android.tools.build/builder-model/2.5.0-alpha-preview-02/constant-values.html


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!

Multiped answered 8/8, 2021 at 21:57 Comment(1)
This is no longer working, see issuetracker.google.com/issues/242730594Introvert

© 2022 - 2024 — McMap. All rights reserved.