我正在 build.gradle
脚本中自定义 Android 应用程序的 APK
文件的名称,如下所示:
android {
defaultConfig {
project.ext.set("archivesBaseName", "MyApplication");
}
}
现在我正在使用产品口味:
android {
productFlavors {
green {
applicationId "com.example.myapplication.green"
}
blue {
applicationId "com.example.myapplication.blue"
}
}
}
有没有办法自定义每个APK的名称?我尝试了
archiveBaseName
和 baseName
但没有成功。最后我想拿出以下文件:
build/outputs/apk/Blue-debug-1.2.1.apk
build/outputs/apk/Blue-debug-unaligned.apk
build/outputs/apk/Blue-release-1.2.1.apk
build/outputs/apk/Blue-release-unaligned.apk
build/outputs/apk/Green-debug-1.2.1.apk
build/outputs/apk/Green-debug-unaligned.apk
build/outputs/apk/Green-release-1.2.1.apk
build/outputs/apk/Green-release-unaligned.apk
尝试将其放入 Android 的 build.gradle 闭包中
buildTypes {
debug {
// debug buildType specific stuff
}
release {
// release buildType specific stuff
}
applicationVariants.all { variant ->
if (variant.buildType.name.equals("release") &&
variant.productFlavors[0].name.equals("green") &&
variant.zipAlign) {
def apk = variant.outputFile;
variant.outputFile = new File(apk.parentFile, "green.apk");
} else if(variant.buildType.name.equals("release") &&
variant.productFlavors[0].name.equals("blue") &&
variant.zipAlign) {
def apk = variant.outputFile;
variant.outputFile = new File(apk.parentFile, "blue.apk");
}
}
}
现在输出应该类似于
green.apk
和 blue.apk
。
这将在 2022 年对你有所帮助。
android {
//........
flavorDimensions "version"
productFlavors {
Free {
dimension "version"
applicationId "com.exampleFree.app"
}
Paid {
dimension "version"
applicationId "com.examplePaid.app"
}
}
applicationVariants.all { variant ->
variant.outputs.all { output ->
def appId = variant.applicationId// com.exampleFree.app OR com.examplePaid.app
def versionName = variant.versionName
def versionCode = variant.versionCode // e.g 1.0
def flavorName = variant.flavorName // e. g. Free
def buildType = variant.buildType.name // e. g. debug
def variantName = variant.name // e. g. FreeDebug
//customize your app name by using variables
outputFileName = "${variantName}.apk"
}
}}
Apk 名称 FreeDebug.apk
对于 Android Gradle Plugin 0.13.+ 你应该使用这样的东西:
android{
buildTypes {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def apk = output.outputFile;
def newName = "mysms-" + variant.baseName.replace("-release", "") + "-" + defaultConfig.versionName + ".apk";
output.outputFile = new File(apk.parentFile, newName);
}
}
}
}
对于 Android Studio 3.0,您必须更改:
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, "whatever.apk")
}
}
致:
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "whatever.apk"
}
}
我是这样做的:
productFlavors {
production {
applicationId "com.example.production"
}
staging {
applicationId "com.example.production.staging"
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
if(variant.productFlavors[0].name.equals("staging")){
output.outputFile = new File(output.outputFile.parent,
output.outputFile.name.replace("app-staging-release", "test"));
}else{
output.outputFile = new File(output.outputFile.parent,
output.outputFile.name.replace("app-production-release", "production"));
}
}
}
}
你需要这个很奇怪,因为默认情况下 apk 文件名已经不同了。
如果您查看here第1346行,您可以看到在outputFile中使用了variantData.variantConfiguration.baseName。
variantData.outputFile = project.file("$project.buildDir/apk/${project.archivesBaseName}-${variantData.variantConfiguration.baseName}.apk")
baseName
的文档是
/**
* Full, unique name of the variant, including BuildType, flavors and test, dash separated.
* (similar to full name but with dashes)
*/
private String mBaseName;
所以运行
gradle assembleFreeDebug
应该会给你一个 ProjectName-free-debug.apk
文件。
但如果情况并非如此,或者您想要不同的文件名,您可以使用以下代码来自定义它。
android {
buildTypes {
debug {}
alpha {}
release {}
}
productFlavors {
free{}
paid{}
}
applicationVariants.all { variant ->
def newApkName = variant.name + "my-custom-addition" + ".apk";
variant.outputFile = new File("${project.buildDir}/outputs/apk/", newApkName);
}
}
这是我上面 Lakshman 答案的变体(呵呵),不知道为什么我需要“variants.outputs.each”,但我做到了。
defaultConfig {
applicationId "my.company.com"
minSdkVersion 16
targetSdkVersion 25
applicationVariants.all { variant ->
if (variant.productFlavors[0].name.equals("VariantA")) {
variant.outputs.each { output ->
def apk = output.outputFile;
output.outputFile = new File(apk.parentFile, "Blue.apk");
}
} else { // Only two variants
variant.outputs.each { output ->
def apk = output.outputFile;
output.outputFile = new File(apk.parentFile, "Green.apk");
}
}
}
}
这就是你需要的
android {
defaultConfig {
……
// custom output apk name
applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "${variant.productFlavors[0].name}-${variant.buildType.name}-${variant.versionName}.apk"
}
}
}
……
}
这对我有用:
在 APK 名称中添加
variant.productFlavors[0].name
。
代码示例:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, "APPNAME_" + variant.productFlavors[0].name + "_" + variant.versionName + ".apk")
}
}
}
}
Android Gradle Plugin 0.13.0 已弃用以下使用的outputFile:
applicationVariants.all { variant ->
def newApkName = variant.name + "my-custom-addition" + ".apk";
variant.outputFile = new File("${project.buildDir}/outputs/apk/", newApkName);
}
对我有用的解决方案是:
android {
...
}
project.archivesBaseName = "AndroidAppGeneric-${_buildVersionNameForMaven}"
我使用以下内容,因此文件不会相互覆盖:
applicationVariants.all { variant ->
variant.outputs.each { output ->
def newApkName = variant.name + "-" + variant.versionName + "(" + variant.versionCode +")" + ".apk";
output.outputFile = new File("${project.projectDir}/outputs/apk/" + variant.name, newApkName);
}
}
这里的每个答案都是一样的,而且已经过时了。很容易做到[风格]-[版本]-[构建类型]:
android {
productFlavors {
green {
applicationId "com.example.myapplication.green"
setProperty("archivesBaseName", "Green-" + defaultConfig.versionName)
}
blue {
applicationId "com.example.myapplication.blue"
setProperty("archivesBaseName", "Blue-" + defaultConfig.versionName)
}
}
}
如果你的版本名称是“1.2.1”,运行 gradle assemble 将产生:
Green-1.2.1-debug.apk
Green-1.2.1-release.apk
Blue-1.2.1-debug-apk
Blue-1.2.1-release.apk
希望这会起作用。
科特林:1.8.20 Android Studio:Android Studio 长颈鹿 | 2022年3月1日 摇篮:gradle-7.5.1-bin.zip
android {
..............
applicationVariants.all { variant ->
def formattedDate = new Date().format('yyyyMMdd')
def flavorName = "${variant.flavorName}"
def buildName = variant.buildType.name
def versionName = variant.versionName
def versionCode = variant.versionCode
def fileName = "${formattedDate}_HELLO_Android_${flavorName}_${buildName}_v${versionName}(${versionCode}).apk"
variant.outputs.all {
outputFileName = fileName
}
}
}
预览:
20230822_HELLO_Android_appStoreLive_debug_v4.2.4(196).apk