安卓开发工具Android Studio中Gradle编程

8
好的,我已经观看了Xavier Ducrohet关于新的Android构建系统的YouTube视频。我甚至已经开始使用Android Studio并对其感到满意。现在我需要自定义构建规则以按照我的方式执行操作,其中之一是自动设置清单文件中的codeVersioncodeName。Xavier在他的幻灯片中展示了如何开始执行此操作。
def getVersionCode() {
    def code = ...
    return code
}

android {
    defaultConfig {
        versionCode getVersionCode()
    }
}

请问有没有好的资源可以帮我填补这些空白呢?
更具体地说,我想运行像 git describe --dirty | sed -e 's/^v//' 这样的脚本来确定 versionName,并且使用 git tag | grep -c ^v 来获取 versionCode
谢谢
更新
我尝试了以下的 gradle.build 脚本,但没有成功。它可以正常构建,但是我的已安装应用程序的 App Info 页面上的版本名称没有改变。
task getVersionName(type:Exec) {
  commandLine '../scripts/version-name.sh'

  //store the output instead of printing to the console:
  standardOutput = new ByteArrayOutputStream()

  //extension method stopTomcat.output() can be used to obtain the output:
  ext.output = {
    return standardOutput.toString()
  }
}

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile project(':Common')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16

        versionName getVersionName()
    }
}

如果我用versionName 'Some Text'替换配置文件中的versionName getVersionName(),那么它就能正常工作了,并且在应用信息中构建名称变为Some Text。那么为什么我的getVersionName函数不起作用呢? 更新2 仍然无法工作-但几乎可以!
Shell脚本:
#/bin/bash

NAME=`git describe --dirty | sed -e 's/^v//'`
COMMITS=`echo ${NAME} | sed -e 's/[0-9\.]*//'`

if [ "x${COMMITS}x" = "xx" ] ; then

    VERSION="${NAME}"

else

    BRANCH=" (`git branch | grep "^\*" | sed -e 's/^..//'`)"
    VERSION="${NAME}${BRANCH}"

fi

logger "Build version: ${VERSION}"

echo ${VERSION}

这样做是有效的,日志行确认了在构建项目时脚本被多次调用。但是版本名称仍然为空。我怀疑问题出在Gradle方面,仍然无法得到标准输出。
task getVersionCode(type: Exec) {
    exec { commandLine '../scripts/version-code.sh' }

    //store the output instead of printing to the console:
    standardOutput = new ByteArrayOutputStream()

    ext.output = {
        return standardOutput.toString()
    }
}

task getVersionName(type: Exec) {
    exec { commandLine '../scripts/grMobile/scripts/version-name.sh' }

    //store the output instead of printing to the console:
    standardOutput = new ByteArrayOutputStream()

    ext.output = {
        return standardOutput.toString()
    }
}

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile project(':Common')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16

        versionCode getVersionCode()
        versionName getVersionName.output()
    }
}

可能您需要将其包装在一个shell脚本中,然后捕获其输出。请参考此处:http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.Exec.html - Dhrubajyoti Gogoi
@Dhrubajyoti 非常感谢你的建议。我尝试了一下,见上面的内容,但是关于Gradle脚本仍然有些东西我还没有掌握。 - Dobbo
2个回答

2

在寻找了一番后,我终于找到了解决方案。

Groovy是build.gradle文件中使用的语言,它允许轻松运行命令。以下是解决方案:

def buildCode
     = file("../scripts/version-code.sh")
         .toString().execute().text.trim().toInteger()
def buildName
     = file("../scripts/version-name.sh")
          toString().execute().text.trim()

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 12
        targetSdkVersion 16

        versionCode buildCode
        versionName buildName
    }
}

使用file()可以引用Gradle中的所有文件。

"<some-command".execute()将运行命令,.text可简单访问stdout。我发现需要运行trim()以删除尾随回车符。我想我本可以在脚本中使用echo -n ${VERSION},但我认为trim()方法更好,因为它允许从命令行运行脚本。

构建脚本只是计算git中发布标签的数量。由于我按照以下形式标记我的发布:'v' <major-no> '.' <minor-no> [ '.' <bug-fix> ],因此它只能计算以小写字母“v”开头后跟任何数字的标签:

#/bin/bash

git tag | grep -c ^v[0-9]

在使用此配置构建之前,请不要忘记创建至少一个发布标签。我通常在项目开始时以以下方式标记:
$ git tag -m "Start of development" v0.0

0

你错过了示例注释。 versionName getVersionName.output() 应该可以工作。

编辑:将您的任务代码更改为以下内容。

task getVersionName(type:Exec) {
  exec { commandLine '../scripts/version-name.sh' }

  ...
}

我对Gradle也是新手,看起来他们有一些BUG或文档不足。因为你尝试的代码恰好来自文档中提供的样例。


感谢@Dhrubajyoti,现在versionName已被替换,因此此修复似乎有效。但是替换的文本为空白。我的脚本有问题吗?用“pwd”替换commandLine也会产生空白版本名称。 - Dobbo
到目前为止,感谢您的帮助。我现在一定很接近了。但是我仍然没有得到输出。如果您可以查看“更新2”并告诉我您认为有什么问题,我将不胜感激。 - Dobbo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接