androidStudio 中 gradle 常用功能

摘要:
1.gradle使用svn当前版本信息。defgetSvnVision(){newByteArrayOutputStream().with Stream{os-˃defresult=exec{executable='svn'args=['info']standardOutput=os}defoutputAsString=os。toString()defmatchLastChangedRev=ou

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')
    }
}

  

  

免责声明:文章转载自《androidStudio 中 gradle 常用功能》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux free 命令下free/available区别30天敏捷生活(4): 撰写个人使命宣言下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

关于Android中so解析那些事

1、Android系统目前支持的CPU架构:ARMv5、ARMv7、x86、MIPS、ARMv8、MIPS64、x86_64,每一种都关联着一个ABI(Application Binary Interface)   2、ABI:定义了二进制文件(尤其是.so文件)如何运行在相应系统平台上,包括使用的指令集、内存对齐、可用的系统函数库。Android系统上,...

android N : UnsatisfiedLinkError 只能访问设置为公用库的so库

在android N上使用 .so作为apk的第三方库的时候,会发生java.lang.UnsatisfiedLinkError: 09-27 12:17:01.280 D/ListenSoundModel( 3635): Load libxxxjni 09-27 12:17:01.292 D/AndroidRuntime( 3635): Shutting...

android studio多渠道多包名多apk打包

转自 利用 Android Studio 和 Gradle 打包多版本APK 搬砖的道路上,经常会有各种不同的需求,比如今天就碰到过一个打包版本的apk的要求, 比如一个apk给多个客户使用,如张三公司 ,李四集团,而我们的服务端的api域名以及处理方式也不一样, 苦逼的方式是一个一个修改包名,代码,但如果我们使用了Android Studio 和 Gra...

(appium+python)UI自动化_02_appium启动手机app

前提:需先安装配置好appium+python自动化环境,已配置好环境的小伙伴可以参考以下步骤启动Android app,具体步骤如下: 一、USB连接手机 (1)手机USB连接电脑 (2)手机打开开发者模式、USB调试功能 二、基础信息配置 1,获取设备号 终端获取设备号,指令:adb devices   2,获取apk包名、activity (1)打开...

关于Gradle配置的小结

前言 使用 Android Studio 来开发 Android 工程的过程中,接触 Gradle 是不可避免的,比如配置签名、引入依赖等。那么 Gradle 到底是什么东西呢? Gradle 是一个基于 Apache Ant 和 Apache Maven 概念的项目自动化建构工具。它使用一种基于 Groovy 的特定领域语言 (DSL) 来声明项目...

Spring boot + Gradle + Eclipse打war包发布总结

首先感谢两位博主的分享 http://lib.csdn.net/article/git/55444?knId=767 https://my.oschina.net/alexnine/blog/540651 buildscript { ext { springBootVersion = '1.5.2.RELEASE' }...