如何使用产品风格和 APK 拆分重命名和生成 Gradle 中的所有 APK 和捆绑包

How to Rename and Generate all APK & Bundle from Gradle with Product flavour and APK Splits

因为我尝试了这两种方法(一次使用一种)来重命名 APK

选项 - 一个

// To Change the APK and Bundle Name
archivesBaseName = "${name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}"

选项 - 两个

(为此还尝试将 - variant.outputs.all 更改为 variant.outputs.each

android.applicationVariants.all { variant ->
    variant.outputs.all { output ->
        output.outputFileName = "${variant.buildType.name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}.apk"
    }
}

当我使用选项 One,

Issue - it generates all splits but it overrides the flavor config with the last flavor written in Gradle.

此外,尝试将选项 One 仅放在 defaultConfig 中一次,但作为在那之后写的 productFlavours 它 returns 中的 nullversionCodeversionName.

productFlavors {
    aFlavor {
        applicationId "com.a"
        
        versionCode 5
        versionName "1.0.5"

        signingConfig signingConfigs.signingA

        // To Change the APK and Bundle Name
        archivesBaseName = "${name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}"
    }
    bFlavor {
        applicationId "com.b"

        versionCode 5
        versionName "1.0.5"

        signingConfig signingConfigs.signingB

        // To Change the APK and Bundle Name
        archivesBaseName = "${name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}"
    }
    cFlavor {
        applicationId "com.c"

        versionCode 3
        versionName "1.0.3"

        signingConfig signingConfigs.signingC

        // To Change the APK and Bundle Name
        archivesBaseName = "${name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}"
    }
}

当我使用选项两个,

Issue - it generates the correct name but generates a single APK file.

splits {
    abi {
        enable true
        reset()
        include 'arm64-v8a', 'x86', 'x86_64'
        universalApk false
    }
}

android.applicationVariants.all { variant ->
    variant.outputs.all { output ->
        output.outputFileName = "${variant.buildType.name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}.apk"
    }
}

Issue for bundle - not able to rename the bundle using option Two.

因为您正在使用 universalApk false,Gradle 为每个 ABI 生成不同的输出 apk。因此,您必须将 ABI 名称添加到输出文件名中。 output.getFilter(com.android.build.OutputFile.ABI) 表达式 returns 当前 ABI 名称。 请看下面的例子:

android.applicationVariants.all { variant ->
    variant.outputs.all { output ->
        output.outputFileName = "${variant.buildType.name}-${output.getFilter(com.android.build.OutputFile.ABI)}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}.apk"
    }
}

如果你想有不同的输出apks,你必须设置不同的风格。

根据的回答,你可以选择Option - Two 稍作改动,如下所述仅适用于 APK,不适用于 Bundle / AAB files

splits {
    abi {
        enable true
        reset()
        include 'arm64-v8a', 'x86', 'x86_64'
        universalApk false
    }
}

android.applicationVariants.all { variant ->
    variant.outputs.all { output ->
        // New one or Updated one
        output.outputFileName = "${variant.getFlavorName()}-${variant.buildType.name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}-${output.getFilter(com.android.build.OutputFile.ABI)}.apk"
        // Old one
        // output.outputFileName = "${variant.buildType.name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}.apk"
    }
}

此外,从每个 Flavor 块中删除该行

// To Change the APK and Bundle Name
archivesBaseName = "${name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}"

这样,你就得到了这样的输出文件名

口味

  • 发布

aFlavor-release-v5_1.0.5-16Jan2020_21-26-arm64-v8a.apk

aFlavor-release-v5_1.0.5-16Jan2020_21-26-x86_64.apk

aFlavor-release-v5_1.0.5-16Jan2020_21-26-x86.apk

  • 调试

aFlavor-debug-v5_1.0.5-16Jan2020_21-26-arm64-v8a.apk

aFlavor-debug-v5_1.0.5-16Jan2020_21-26-x86_64.apk

aFlavor-debug-v5_1.0.5-16Jan2020_21-26-x86.apk

对于 bFlavor

与上面类似的名称只需将前缀aFlavor更改为bFlavor like

bFlavor-release-v5_1.0.5-16Jan2020_21-26-arm64-v8a.apk

对于 cFlavor

与上面类似的名称只需将前缀aFlavor更改为cFlavor 并且,versionCodeversionName

cFlavor-release-v3_1.0.3-16Jan2020_21-26-arm64-v8a.apk

string.xml 文件中删除 app_name

apply plugin: 'com.android.application'
android {
    signingConfigs {
        release {
            keyAlias 'your key alias'
            keyPassword 'your password'
            storeFile file('path of your keystore')
            storePassword 'your password'
        }
    }
    compileSdkVersion 28
    flavorDimensions "default"
    project.archivesBaseName = "ProjectName";
    defaultConfig {
        applicationId "Your package name"
        minSdkVersion 16
        targetSdkVersion 28
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        signingConfig signingConfigs.release
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationVariants.all { variant ->
                variant.outputs.all { //output ->

                    outputFileName = "YourAppName-${variant.baseName}-${variant.versionName}.apk"
                }
            }
        }
        debug {

        }
    }
    productFlavors {
        dev {
            versionCode 778899 // your versioncode
            versionName "v.1.1.BUILD_NUM"  // your version name
            applicationIdPrefix ".dev" // your application package name like as com.a
            resValue "string", "app_name", "Your App Name"

        }
        live {
            versionCode 778899 // your versioncode
            versionName "v.1.1.BUILD_NUM"  // your version name
            applicationIdPrefix ".dev" // your application package name like as com.a
            resValue "string", "app_name", "Your App Name"

        }
    }
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}

dependencies {
   // Here your application gradle
}

这些解决方案不适用于 aab(捆绑包)。仅 APK。