Reading versionName from build.gradle in bash

Is there a way to read a value versionNamefrom an build.gradleAndroid project file to use it in bash?

More precisely: how can I read this value from a file and use it in a Travis-CI script? I will use this as

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

I set up Travis-CI, as described in this report qaru.site/questions/382980 / ... .

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

+8
source share
9 answers

Having deployed the answer to Khozzy to get the Name version of your Android package from build.gradle, add this custom task:

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

and call it like this:

gradle -q printVersionName
+8

, ..

task printVersion {
    doLast {
        println project.version
    }
}

Bash:

$ gradle -q pV
1.8.5
+9

alnet ( ):

# 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
+8

?

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

-o grep , , , awk, versionCode NUMBER.

+6

,

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)
            }
        }
    }
}
}

${getVersionName()} build.gradle

+1

Kotlin DSL, :

tasks.create("printVersionName") {
    doLast { println(version) }
}
+1

, - :

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

,

./gradlew -q printVersion -PflavorName=MyFlavor
+1
source

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

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

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"will give me8.1.9

+1
source

Source: https://habr.com/ru/post/1629320/


All Articles