如何将 MD5 添加到带有 Gradle 的 apk 名称

How to add MD5 to the name of apk with Gradle

对于我们的命名约定,我们需要在 apk 名称的末尾添加文件的 MD5。 我试着用这个好玩

static def generateMD5(final file) {
MessageDigest digest = MessageDigest.getInstance("MD5")
file.withInputStream(){is->
    byte[] buffer = new byte[8192]
    int read = 0
    while( (read = is.read(buffer)) > 0) {
        digest.update(buffer, 0, read);
    }
}
byte[] md5sum = digest.digest()
BigInteger bigInt = new BigInteger(1, md5sum)
return bigInt.toString(16).padLeft(32, '0')

并在应用程序变体任务中

android.applicationVariants.all { variant ->
    variant.outputs.all { output ->
        output.outputFileName = "MyApp v${variant.versionName}-${generateMD5(output.outputFile)}.apk"
    }
}

问题是APK文件不是在这个任务中创建的,所以我应该在哪里修改文件名?

最后,我用 jenkins 和 shell 脚本完成了。谢谢!