Подтвердить что ты не робот

Как настроить имя файла APK для ароматов продукта?

Я настраиваю имя файла APK моего приложения Android в build.gradle script следующим образом:

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
4b9b3361

Ответ 1

Попробуйте поместить это в ваше закрытие 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.

Ответ 2

Для Android Gradle Плагин 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);
            }
        }
    }
}

Ответ 3

Для Android Studio 3.0 вы должны изменить:

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        output.outputFile = new File(output.outputFile.parent, "whatever" + ".apk")
    }
}

To:

android.applicationVariants.all { variant ->
        variant.outputs.all { 
            outputFileName = "whatever" + ".apk")
    }
}

Ответ 4

Я сделал это вот так:

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

            }
        }
    }

Ответ 5

Странно, что вам понадобится это, потому что имя файла apk уже по-разному.

Если вы посмотрите здесь в строке 1346, вы увидите, что в файле outputFile используется вариантData.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);
    }
}

Ответ 6

Эта работа для меня:

Добавьте 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")

            }
        }
    }
}

Ответ 7

Здесь мой вариант (хеш) Лакшмана отвечает выше, не уверен, зачем мне нужны "варианты .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");
            }
        }
    }
}

Ответ 8

Android Gradle В плагине 0.13.0 устарел выходной файл, который используется в:

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

Ответ 9

Я использую следующее, чтобы файлы не переопределяли друг друга:

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

Ответ 10

Это то, что вам нужно

android {
    defaultConfig {
        ……

        // custom output apk name
        applicationVariants.all { variant ->
            variant.outputs.all {
                outputFileName = "${variant.productFlavors[0].name}-${variant.buildType.name}-${variant.versionName}.apk"
            }
        }
    }
    ……
}