从apk文件名中包含版本名称

从apk文件名中包含版本名称

本文介绍了Android / gradle:从apk文件名中包含版本名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了类似的问题,但是对于我想要做的事情没有什么效果。我正在开发Android Studio和Gradle,并且我在构建文件中有几个版本,每个版本都有一个版本名称。有没有一种方法在APK文件中包含风味的版本名称?

编辑:对于现代版本的Gradle工具,使用@ ptx的变体(使用 output.outputFile 替换 variant.outputFile

  android {
applicationVariants.all {variant - >
variant.outputs.each {输出 - >
输出。 outputFile = new File(output.outputFile.parentFile,
output.outputFile.name.replace(。apk,$ {variant.versionName} .apk));
}
}
}






自Gradle工具开始插件版本0.14,您应该开始使用以下代替来正确支持多个输出文件(例如APK Splits):

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

对于旧版本: p>




是的,我们在我们的应用中执行此操作。只需在build.gradle的android标签中添加以下代码:

  android {
applicationVariants.all {variant - > ;
def oldFile = variant.outputFile
def newPath = oldFile.name.replace(。apk,$ {variant.versionName} .apk)
variant.outputFile = new File( oldFile.parentFile,newPath)
}
}


I have found similar questions but nothing that quite works for what I want to do. I'm developing with Android Studio and gradle, and I have several flavors in my build file, each of which has a versionName. Is there a way to include the flavor's version name in the APK file?

解决方案

EDIT: For modern versions of the Gradle tools, use @ptx's variation (use output.outputFile in place of variant.outputFile:

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


As of Gradle tools plugin version 0.14, you should start using the following instead to properly support multiple output files (e.g. APK Splits):

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

For older versions:


Yeah, we do this in our app. Just add the following in your android tag in build.gradle:

android {
    applicationVariants.all { variant ->
        def oldFile = variant.outputFile
        def newPath = oldFile.name.replace(".apk", "${variant.versionName}.apk")
        variant.outputFile = new File(oldFile.parentFile, newPath)
    }
}

这篇关于Android / gradle:从apk文件名中包含版本名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:59