那些好玩的gradle

1.Apk拷贝
当我需要把编译出来的Apk统一放到服务器或者某个地方的时候,这个脚本就可以起到作用。

task('copyAll') {
    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            if (!output.outputFile.name.contains('unsigned')) {
                task("copy${variant.name.capitalize()}", type: Copy) {
                    from(output.outputFile)
                    into("/home/huanglei/Download/a/")
                }
                dependsOn("copy${variant.name.capitalize()}")
            }
        }
    }
}

2.编译后希望打开apk所在位置

task('openApk') {
    android.applicationVariants.all { variant ->
        variant.assemble.doLast {
            //If this is a 'release' build, reveal the compiled apk in finder/explorer
            if (variant.buildType.name.contains('debug')) {

                def path = null;
                variant.outputs.each { output ->
                    path = output.outputFile
                }

                exec {
                    if (System.properties['os.name'].toLowerCase().contains('mac os x')) {
                        ['open', '-R', path].execute()
                    } else if (System.properties['os.name'].toLowerCase().contains('windows')) {
                        ['explorer', '/select,', path].execute()
                    } else if (System.properties['os.name'].toLowerCase().contains('linux')) {
                        ['nautilus', path].execute()
                    }
                }
            }
        }
    }
}

3.根据git的计较次数作为VersionCode,可以通过 adb shell dumpsys package com.example.samples 来直接查看versionCode

defaultConfig {
    versionCode gitCommitCount() ?: 0
}

def gitCommitCount() {
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'rev-list', '--count', 'HEAD'
            standardOutput = stdout
        }
        def commitCount = stdout.toString().trim().toInteger()
        return commitCount
    }
    catch (ignored) {
        return 0;
    }
}

4.根据Git的tag description来设置versionName

defaultConfig {
    versionName getGitTagName() ?: "1.0.0"
}

def getGitTagName() {
    try {
        def branchout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD'
            standardOutput = branchout
        }
        def branch = branchout.toString().trim()

        if (branch.equals("master")) {
            def stdout = new ByteArrayOutputStream()
            exec {
                commandLine 'git', 'describe', '--dirty'
                standardOutput = stdout
            }
            return stdout.toString().trim()
        } else {
            return branch;
        }
    }
    catch (ignored) {
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值