Read versionName from build.gradle in bash
Asked Answered
M

11

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://mcmap.net/q/581697/-travis-ci-auto-tag-build-for-github-release.

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

Marmoset answered 18/2, 2016 at 7:34 Comment(4)
grep "versionName" build.gradle | awk '{print $2}'Decry
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}')Marmoset
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?Plowshare
As a workaround you can get versionCode from the apk when it will be ready: gist.github.com/j796160836/6ad39ba143bf038bfde8Dorcasdorcea
R
30

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
Ruckus answered 22/11, 2017 at 14:38 Comment(1)
./gradlew -q printVersionNameChlorothiazide
E
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
Entreaty answered 22/10, 2016 at 7:55 Comment(4)
this prints "unspecified" in my project.Gradeigh
@dan replace "project.version" with "android.defaultConfig.versionName"Tehuantepec
I don't understand this answer. How is the task being called?Superstitious
@Superstitious take a look at Gradle Task Abbreviation - docs.gradle.org/current/userguide/…Entreaty
L
10

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

grep "versionName" app/build.gradle | awk '{print $2}' | tr -d \''"\'
Littrell answered 19/6, 2019 at 21:43 Comment(2)
It will not work for case when there are few flavors or different versions for different build variants.Dorcasdorcea
Thanks, I added an additional param to get a clean version name: grep -m1 "versionName" app/build.gradle | awk '{print $2}' | tr -d \''"\'Bryant
M
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
Marmoset answered 18/2, 2016 at 22:22 Comment(1)
It will not work for case when there are few flavors or different versions for different build variants.Dorcasdorcea
R
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.

Recce answered 31/12, 2017 at 11:42 Comment(1)
It will not work for case when there are few flavors or different versions for different build variants.Dorcasdorcea
B
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
Bostow answered 13/2, 2019 at 16:47 Comment(0)
B
2

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

tasks.create("printVersionName") {
    doLast { println(version) }
}
Bestialize answered 14/10, 2018 at 19:53 Comment(0)
A
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

Arlindaarline answered 18/2, 2016 at 8:0 Comment(1)
Somehow alnets' very short solution works.Marmoset
S
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

Selfrestraint answered 15/10, 2019 at 13:58 Comment(2)
This is the only answer working for versionName (without using gradle), thank youFarcy
It will not work for case when there are few flavors or different versions for different build variants.Dorcasdorcea
P
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
Pronoun answered 6/2, 2023 at 9:15 Comment(0)
B
0

If you're using a kotlin based buildfile (build.gradle.kts) you could solve it like this:

grep -o -E 'versionName = "[[:digit:]\.]+"' app/build.gradle.kts | grep -o -E '[[:digit:]\.]+'

I prefer Grep over Gradle, because Gradle is sooooo slow. (At least in my CD pipeline.)

Whats going on in detail?

Grep is a common Posix utility to search for specific lines in text files. However it can do some pretty neat things.

-o returns only the matched text.
-E makes Grep use advanced regex.

In step 1 we read the build.gradle.kts file and search for the line, that contains our version number.

grep -o -E 'versionName = "[[:digit:]\.]+"' app/build.gradle.kts

returns something like

versionName = "1.0"

In the next step we're piping this output to another grep's STDIN using a pipe symbol (|). There we match for the version number only. The regex [[:digit:]\.]+ matches for one or more of these characters: 0123456789. - which should be everything I expect to be in a version number.

Brouhaha answered 27/3 at 20:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.