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;
}
}