从 bash 中的 build.gradle 读取 versionName
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35475432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read versionName from build.gradle in bash
提问by SUhrmann
Is there a way to read the value versionName
from the build.gradle
file of an Android project to use it in bash?
有没有办法versionName
从build.gradle
Android 项目的文件中读取值以在 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
更准确地说:如何从文件中读取此值并在 Travis-CI 脚本中使用它?我会像这样使用它
# 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.
我设置了一个 Travis-CI,如这篇文章https://stackoverflow.com/a/28230711/1700776 中所述。
My build.gradle: http://pastebin.com/uiJ0LCSk
我的 build.gradle:http://pastebin.com/uiJ0LCSk
采纳答案by Alex Suzuki
Expanding on Khozzy's answer, to retrieve versionName of your Android package from the build.gradle, add this custom task:
扩展 Khozzy 的答案,要从 build.gradle 检索 Android 包的 versionName,请添加以下自定义任务:
task printVersionName {
doLast {
println android.defaultConfig.versionName
}
}
and invoke it so:
并调用它:
gradle -q printVersionName
回答by Khozzy
You can define a custom task, i.e.
您可以定义自定义任务,即
task printVersion {
doLast {
println project.version
}
}
And execute it in Bash:
并在 Bash 中执行它:
$ gradle -q pV
1.8.5
回答by SUhrmann
Thanks to alnet'scomment I came up with this solution (note Doug Stevenson's objection):
感谢alnet 的评论,我想出了这个解决方案(注意Doug Stevenson 的反对意见):
# 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 }') # 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
回答by hasen
How about this?
这个怎么样?
grep -o "versionCode\s\+\d\+" app/build.gradle | awk '{ print }'
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
.
该-o
选项使 grep 仅打印匹配的部分,因此您可以保证传递给awk
的只是 pattern versionCode NUMBER
。
回答by ebaynaud
I like this one liner to get only the version name without quotes:
我喜欢这个 liner 只获取不带引号的版本名称:
grep "versionName" app/build.gradle | awk '{print }' | tr -d \''"\'
回答by Kevin ABRIOUX
grep versionName :
grep 版本名称:
For MAC:
对于 MAC:
grep -o "versionName\s.*\d.\d.\d" app/build.gradle | awk '{ print $2 }' | tr -d \''"\'
grep -o "versionName\s.*\d.\d.\d" app/build.gradle | awk '{ print $2 }' | tr -d \''"\'
For Unbuntu:
对于 Unbuntu:
grep -o "versionName\s\+.*" app/build.gradle | awk '{ print $2 }' | tr -d \''"\'
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
所以在 build.gradle 中输入versionName "8.1.9"
给我8.1.9
回答by Bruce Lee
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
用于${getVersionName()}
在 build.gradle 中获取版本
回答by matfax
If you are seeking a migrated version for Kotlin DSL, here is with what I came up:
如果您正在寻找 Kotlin DSL 的迁移版本,以下是我的想法:
tasks.create("printVersionName") {
doLast { println(version) }
}
回答by Clive Jefferies
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