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
You can define a custom task, ie
task printVersion {
doLast {
println project.version
}
}
And execute it in Bash:
$ gradle -q pV
1.8.5
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
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
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
.
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
If you are seeking a migrated version for Kotlin DSL, here is with what I came up:
tasks.create("printVersionName") {
doLast { println(version) }
}
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
我喜欢这种衬板,只得到不带引号的版本名称:
grep "versionName" app/build.gradle | awk '{print $2}' | tr -d \''"\'
grep versionName :
grep -o "versionName\\s.*\\d.\\d.\\d" app/build.gradle | awk '{ print $2 }' | tr -d \\''"\\'
So input in build.gradle versionName "8.1.9"
give me 8.1.9
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.