androidStudio 中 gradle 常用功能

1. gradle 使用 svn 当前版本信息.

def getSvnRevision() {
    new ByteArrayOutputStream().withStream { os ->
        def result = exec {
            executable = 'svn'
            args = ['info']
            standardOutput = os
        }
        def outputAsString = os.toString()
        def matchLastChangedRev = outputAsString =~ /Last Changed Rev: (d+)/

        ext.svnRev = "${matchLastChangedRev[0][1]}".toInteger()
    }

    return svnRev
}

使用例子: 

versionCode 1
versionName "0.${versionCode}." + getSvnRevision()

使用 git checkout 的 6位短版本信息.

task gitReversion {
    def cmd = "git rev-parse --short HEAD"
   // git rev-list --all | wc -l 获取提交次 def proc = cmd.execute() ext.revision = proc.text.trim() }

使用例子:

versionCode 1
versionName "0.${versionCode}." + gitReversion.revision

  

gradle 拷贝文件:

task copyTaskWithPatterns(type: Copy) {
    from "${buildDir}/outputs/apk/"
    into "c:/apks/"

// 不拷贝未签名的文件. exclude { details -> details.file.name.endsWith('-unaligned.apk') || details.file.name .endsWith('-unsigned.apk') } include "**/*.apk" println "apk copied. ${buildDir}" } build.doLast { tasks.copyTaskWithPatterns.execute() }

其中注意的是 如果偷懒写法的话, exclude 在include之前.

如下的 build 文件指定输出的文件名.

    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }

        debug {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            debuggable true
            jniDebugBuild true
        }

        applicationVariants.all { variant ->

            def file = variant.outputFile
            if(variant.buildType.name.equals("release")){ // 判断编译的类型
                variant.outputFile = new File(
                        (String) file.parent,
                        (String) (project.archivesBaseName + "-" + variant.mergedFlavor.versionName
                                + ".apk")
                )
            }else{
                variant.outputFile = new File(
                        (String) file.parent,
                        (String) (project.archivesBaseName + "-" + variant.mergedFlavor.versionName
                                + ".apk")
                )
            }
        }
    }

  

另外

 variant.baseName = {moduleName}-debug,
 project.archivesBaseName ={projectName}
 variant.name={moduleName}Debug

 关于 android-studio中 gradle 的使用方式. 参见: http://tools.android.com/tech-docs/new-build-system/user-guide

如下 表示 自定义task,  callSignBAT  在运行此task 之前, 必须执行 copyToSign 这个task, 

task callSignBAT(type: Exec, dependsOn: "copyToSign") {
    println(">>>start call sign script....")

    def command = 'Sign_MUI.bat'
    if (!file("sign/src/${apkName}").exists()) {
        command = 'exit'
    }
    println(">>>exec command:" + command)

    workingDir 'sign'
    commandLine 'cmd', '/c', command

    standardOutput = new ByteArrayOutputStream()

    doLast {
        checkExecResult(execResult, 'Error', standardOutput);
    }

    println(">>call sign script finished.")
}

def checkExecResult(execResult, failText, standardOutput){
    println("execResult:" + execResult)
    println("standardOutput:"+standardOutput.toString())
    if (execResult) {
        if (execResult.getExitValue() != 0) {
            throw new GradleException('Non-zero exit value: ' + execResult.getExitValue())
        }
        if (standardOutput.toString().contains(failText)){
            throw new GradleException('"' + failText + '" string in output: ' + standardOutput.toString())
        }
    } else {
        throw new GradleException('Returned a null execResult object')
    }
}

  

  

作者:闵天
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文地址:https://www.cnblogs.com/checkway/p/4103819.html