24

Is there a way to read the value versionName from the build.gradle file of an Android project to use it in bash?

More precisely: How can I read this value from the file and use it in a Travis-CI script? I'll use it like

# ANDROID_VERSION=???
export GIT_TAG=build-$ANDROID_VERSION

I set up a Travis-CI like described in this post https://stackoverflow.com/a/28230711/1700776.

My build.gradle: http://pastebin.com/uiJ0LCSk

Community
  • 1
  • 1
SUhrmann
  • 632
  • 1
  • 8
  • 26
  • 8
    grep "versionName" build.gradle | awk '{print $2}' – alnet Feb 18 '16 at 07:37
  • 3
    Thank you - it works! Actually the file is in ``./app/build.gradle`` so the resulting ``.travis.yml`` line is ``- export VERSION=$(grep "versionName" ./app/build.gradle | awk '{print $2}')`` – SUhrmann Feb 18 '16 at 07:50
  • 1
    This will not work in all cases. What if there are build variants that have different version names? What if the build.gradle uses versionNameSuffix to modify version names per flavor? – Doug Stevenson Feb 18 '16 at 08:20
  • 1
    As a workaround you can get versionCode from the apk when it will be ready: https://gist.github.com/j796160836/6ad39ba143bf038bfde8 – Yazon2006 Jan 15 '20 at 16:17

10 Answers10

29

Expanding on Khozzy's answer, to retrieve versionName of your Android package from the build.gradle, add this custom task:

task printVersionName {
    doLast {
        println android.defaultConfig.versionName
    }
}

and invoke it so:

gradle -q printVersionName
Alex Suzuki
  • 1,083
  • 10
  • 18
16

You can define a custom task, i.e.

task printVersion {
    doLast {
        println project.version
    }
}

And execute it in Bash:

$ gradle -q pV
1.8.5
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
Khozzy
  • 1,064
  • 4
  • 15
  • 29
9

Thanks to alnet's comment I came up with this solution (note Doug Stevenson's objection):

# variables
export GRADLE_PATH=./app/build.gradle   # path to the gradle file
export GRADLE_FIELD="versionName"   # field name
# logic
export VERSION_TMP=$(grep $GRADLE_FIELD $GRADLE_PATH | awk '{print $2}')    # get value versionName"0.1.0"
export VERSION=$(echo $VERSION_TMP | sed -e 's/^"//'  -e 's/"$//')  # remove quotes 0.1.0
export GIT_TAG=$TRAVIS_BRANCH-$VERSION.$TRAVIS_BUILD_NUMBER
# result
echo gradle version: $VERSION
echo release tag: $GIT_TAG
Community
  • 1
  • 1
SUhrmann
  • 632
  • 1
  • 8
  • 26
  • 2
    It will not work for case when there are few flavors or different versions for different build variants. – Yazon2006 Jan 15 '20 at 13:02
9

I like this one liner to get only the version name without quotes:

grep "versionName" app/build.gradle | awk '{print $2}' | tr -d \''"\'
ebaynaud
  • 682
  • 6
  • 15
8

How about this?

grep -o "versionCode\s\+\d\+" app/build.gradle | awk '{ print $2 }'

The -o option makes grep only print the matching part so you're guaranteed that what you pass to awk is only the pattern versionCode NUMBER.

hasen
  • 161,647
  • 65
  • 194
  • 231
  • It will not work for case when there are few flavors or different versions for different build variants. – Yazon2006 Jan 15 '20 at 13:01
3

If you have multiple flavors and set the version information in the flavor, you can use something like this:

task printVersion{
doLast {
    android.productFlavors.all {
        flavor ->
            if (flavorName.matches(flavor.name)) {
                print flavor.versionName + "-" + flavor.versionCode
            }
    }
}
}

You can then call it using

./gradlew -q printVersion -PflavorName=MyFlavor
Clive Jefferies
  • 1,138
  • 14
  • 26
1

eg

android{
  android.applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def outputFile = output.outputFile
        def fileName
        if (outputFile != null && outputFile.name.endsWith('.apk')) {
            if (!outputFile.name.contains('unaligned')) {
                fileName = "yourAppRootName_${variant.productFlavors[0].name}_${getVersionName()}_${variant.buildType.name}.apk"
                output.outputFile = new File(outputFile.parent + "/aligned", fileName)
            }
        }
    }
}
}

use ${getVersionName()} to get version in build.gradle

Bruce Lee
  • 4,177
  • 3
  • 28
  • 26
  • Somehow [alnets'](http://stackoverflow.com/questions/35475432/read-versionname-from-build-gradle-in-bash/35475826#comment58645220_35475432) very short solution works. – SUhrmann Feb 18 '16 at 08:02
1

If you are seeking a migrated version for Kotlin DSL, here is with what I came up:

tasks.create("printVersionName") {
    doLast { println(version) }
}
matfax
  • 634
  • 11
  • 17
1

grep versionName :

For MAC :

grep -o "versionName\s.*\d.\d.\d" app/build.gradle | awk '{ print $2 }' | tr -d \''"\'

For Unbuntu :

grep -o "versionName\s\+.*" app/build.gradle | awk '{ print $2 }' | tr -d \''"\'

So input in build.gradle versionName "8.1.9" give me 8.1.9

Kevin ABRIOUX
  • 16,507
  • 12
  • 93
  • 99
0

Another solution would be to use a gradle.properties file.

  1. Set your value in gradle.properties
VERSION_NAME=1.0 with more
  1. Update your gradle.build to get the value from the gradle.properties
versionName VERSION_NAME
  1. grep your gradle.properties file rather than gradle.build
grep "VERSION_NAME" app/gradle.properties | cut -d "=" -f 2
usilo
  • 305
  • 2
  • 10