Android Studio Gradle编程

Dob*_*bbo 8 gradle android-studio build.gradle

好的,我在新的Android构建系统上观看了Xavier Ducrohet的YouTube视频.我甚至转而使用Android Studio并对此感到满意.现在我需要自定义构建规则以按照我想要的方式执行操作,其中一个是自动设置清单文件中的codeVersioncodeName.

Xavier在他的一张幻灯片中展示了如何做到这一点:

def getVersionCode() {
    def code = ...
    return code
}

android {
    defaultConfig {
        versionCode getVersionCode()
    }
}
Run Code Online (Sandbox Code Playgroud)

那么有些人可以如此友善地指出我有充足的资源来填补空白点吗?

更具体一点,我想运行一个类似于git describe --dirty | sed -e 's/^v//'确定versionNamegit 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()
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我更换配置versionName getVersionName()versionName 'Some Text'那么它的工作原理和内部版本名称将成为Some Text在App信息.那么为什么我的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}
Run Code Online (Sandbox Code Playgroud)

这样做,并且日志行确认在制作项目时多次调用脚本.但是versionName仍然是空白的.我怀疑Gradle方面仍然没有得到stdout.

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()
    }
}
Run Code Online (Sandbox Code Playgroud)

Dob*_*bbo 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
    }
}
Run Code Online (Sandbox Code Playgroud)

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]
Run Code Online (Sandbox Code Playgroud)

在使用此配置进行构建之前,请不要忘记创建至少一个发布标签。我通过以下方式在项目开始时进行标记:

$ git tag -m "Start of development" v0.0
Run Code Online (Sandbox Code Playgroud)