常用函数与属性
日期函数:
def releaseTime() {
return new Date().format("yyyyMMddHHmm", TimeZone.getTimeZone("Asia/Shanghai"))
}
MD5函数:
import java.security.MessageDigest
def generateMD5(String path) {
MessageDigest digest = MessageDigest.getInstance("MD5")
FileInputStream fis= new FileInputStream(path)
ByteArrayOutputStream bos = new ByteArrayOutputStream()
byte[] b = new byte[1024]
int n
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n)
}
fis.close()
bos.close()
digest.update(bos.toByteArray())
return new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')
}
项目根目录:
${rootDir.absolutePath}
用法:
android {
applicationVariants.all { variant ->
//最后输出的apk、产物按规则命名:
variant.outputs.all { output ->
outputFileName = "xxxx_${variant.versionName}(${variant.versionCode})_${variant.buildType.name}__${releaseTime()}.apk"
}
//在产物目录下创建一个MD5文件
variant.assemble.doLast {
variant.outputs.each { output ->
println "------------------Start-----------------------"
File aligned = output.outputFile
String md5 = generateMD5(aligned.absolutePath)
println md5
File md5File = new File(aligned.getParent()+File.separator+md5)
md5File.createNewFile()
println "------------------END-----------------------"
}
}
}
}