如何添加使用0.14+版本的Andr​​oid摇篮插件的版本号到我的APK文件?我的、摇篮、版本号、插件

2023-09-07 16:59:03 作者:哥,依旧颓废

我想有的versionName 包含在输出的名义APK文件从我的Andr​​oid版本。

I want to have the versionName included in the name of the output APK files from my Android build.

这里有一个另一个答案与pre-0.14.x工作插件版本,但他们改变了一些数据模型,使不工作了,我无法弄清楚如何解决它。据我所知,code的下方大块应该工作,但去年设置()调用似乎没有任何效果。它不会抛出一个错误,但价值也不会被取代。

There's an another answer that works with pre-0.14.x plugin versions, but they changed some of the data model so that doesn't work anymore, and I couldn't figure out how to fix it. As far as I can tell, the below chunk of code should work, but that last set() call appears to have no effect. It doesn't throw an error, but the value isn't replaced either.

buildTypes {
    applicationVariants.all { variant ->
        def oldFile = variant.outputs.outputFile.get(0)
        def newFile = new File(
                oldFile.parent,
                oldFile.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))

        variant.outputs.outputFile.set(0, newFile)
}

有人可以帮我这个?

Can someone help me out with this?

推荐答案

您现在需要多一个循环,每个变种可以有多个输出:

You need one more loop now that each variant can have multiple outputs:

android {
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File(
                    output.outputFile.parent,
                    output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
        }
    }
}