我正在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
答案 0 :(得分:24)
尝试将其放入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.apk
和blue.apk
。
答案 1 :(得分:9)
对于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);
}
}
}
}
答案 2 :(得分:9)
对于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")
}
}
答案 3 :(得分:5)
我是这样做的:
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"));
}
}
}
}
答案 4 :(得分:3)
你需要这个很奇怪,因为默认情况下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);
}
}
答案 5 :(得分:3)
这将在2020年为您提供帮助。
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
答案 6 :(得分:1)
这项工作对我来说:
在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")
}
}
}
}
答案 7 :(得分:1)
这就是您需要的
android {
defaultConfig {
……
// custom output apk name
applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "${variant.productFlavors[0].name}-${variant.buildType.name}-${variant.versionName}.apk"
}
}
}
……
}
答案 8 :(得分:1)
这里的每个答案都是一样的,而且已经过时了。 [flavor]-[version]-[build type]很容易:
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汇编将产生:
Green-1.2.1-debug.apk
绿色1.2.1-release.apk
Blue-1.2.1-debug-apk
Blue-1.2.1-release.apk
答案 9 :(得分:0)
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}"
答案 10 :(得分: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);
}
}
答案 11 :(得分:0)
这是Lakshman上面回答的变体(heh),不知道为什么我需要" 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");
}
}
}
}