如何自定义产品风味的 APK 文件名?

我正在 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的名称?我尝试了 archiveBaseNamebaseName 没有成功。最后,我想提出以下文件:

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
stack overflow How to customize the APK file name for product flavors?
原文答案

答案:

作者头像

尝试把它放在你的 build.gradle 的 android 闭包中

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.apkblue.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

证明 [enter image description here ](https://i.stack.imgur.com/l9cwu.png

[enter image description here ](https://i.stack.imgur.com/xtyji.png

作者头像

对于 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文件名已经不同

如果您查看第 1346 行的 here ,您可以看到在 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);
    }
}
作者头像

这对我有用:

在 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")

            }
        }
    }
}
作者头像

这是我上面 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"
            }
        }
    }
    ……
}
作者头像

Android Gradle 插件 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}"