48

我正在脚本中自定义我的 Android 应用程序的 APK 文件的名称,build.gradle如下所示:

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
4

12 回答 12

27

尝试把它放在你的 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

于 2014-08-04T08:17:18.737 回答
17

这将在 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

证明 在此处输入图像描述

在此处输入图像描述

于 2020-01-03T17:46:48.613 回答
10

对于 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);
            }
        }
    }
}
于 2014-10-01T13:37:46.447 回答
10

对于 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")
    }
}
于 2017-10-26T16:50:33.447 回答
6

我是这样做的:

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"));
                }

            }
        }
    }
于 2015-08-05T11:35:52.423 回答
3

你需要这个很奇怪,因为默认情况下apk文件名已经不同

如果您在此处查看第 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);
    }
}
于 2014-08-05T11:20:27.887 回答
2

这是我上面 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");
            }
        }
    }
}
于 2017-07-09T01:31:52.177 回答
2

这就是你需要的

android {
    defaultConfig {
        ……

        // custom output apk name
        applicationVariants.all { variant ->
            variant.outputs.all {
                outputFileName = "${variant.productFlavors[0].name}-${variant.buildType.name}-${variant.versionName}.apk"
            }
        }
    }
    ……
}
于 2019-10-09T16:37:22.080 回答
1

这对我有用:

variant.productFlavors[0].name在 APK 名称中添加。

代码示例:

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

            }
        }
    }
}
于 2017-03-07T10:43:55.650 回答
1

这里的每个答案都是一样的,而且已经过时了。很容易做到[风味]-[版本]-[构建类型]:

android {
    productFlavors {
        green {
            applicationId "com.example.myapplication.green"
            setProperty("archivesBaseName", "Green-" + defaultConfig.versionName)
        }

        blue {
            applicationId "com.example.myapplication.blue"
            setProperty("archivesBaseName", "Blue-" + defaultConfig.versionName)
        }
    }
}

如果您的 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

于 2020-03-24T19:19:52.317 回答
0

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}"
于 2014-09-19T02:54:21.237 回答
0

我正在使用以下内容,因此文件不会相互覆盖:

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);
    }
}
于 2016-12-22T11:29:09.523 回答