14

As I have tried these 2 ways (using a single at a time) to rename the APK

Option - One

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

Option - Two

(for this also tried to change the - variant.outputs.all to 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"
    }
}

When I use option One,

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

Also, try to put option One only once in defaultConfig but as productFlavours written after that it returns the null value in versionCode and versionName.

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

When I use option Two,

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.

Mihir Trivedi
  • 1,458
  • 18
  • 39
  • 1
    http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.ProductFlavor.html why not to use some standard properties like `versionNameSuffix` or `applicationIdSuffix` ? – daggett Jan 13 '20 at 18:17
  • @daggett Agree, but what if application ID and version name are totally different? – Mihir Trivedi Jan 16 '20 at 11:45

6 Answers6

5

As per This answer, you can go with Option - Two with minor changes as mentioned below only works for APK, not the Bundle / AAB files (for bundle refer to this answer)

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

Also, remove the line from each Flavor's block

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

By this, you get the output file name like this

For aFlvour

  • Release

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

  • Debug

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

For bFlavor

Similar name as above just change the prefix aFlavor with bFlavor like

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

For cFlavor

Similar name as above just change the prefix aFlavor with cFlavor and, versionCode and versionName as respected

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

Mihir Trivedi
  • 1,458
  • 18
  • 39
2

Because you are using universalApk false, Gradle generates different output apk for each ABI. So you have to add ABI name to your output filename. The output.getFilter(com.android.build.OutputFile.ABI) expression returns current ABI name. Please look at the following example:

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"
    }
}
Mir Milad Hosseiny
  • 2,769
  • 15
  • 19
2

These solutions do not work for aab (bundle). Only APK.

chitgoks
  • 311
  • 2
  • 6
  • 17
  • Yes, as mentioned here https://stackoverflow.com/a/59774018/1684864 and for Bundle, I also ask the question but it is marked as duplicate https://stackoverflow.com/q/63533131/1684864 – Mihir Trivedi May 17 '22 at 15:14
  • See my solution which works for both bundle/apk – 3c71 Jul 08 '23 at 12:54
1

Remove app_name from string.xml file

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
}
Mihir Trivedi
  • 1,458
  • 18
  • 39
0

This works for bundle or APK. But I can't make it work for both without commenting in/out the necessary lines. If anyone has an idea about that?

        applicationVariants.all { variant ->
            variant.outputs.all {
                if (variant.buildType.name == "release") {
        //              outputFileName = createCopyTaskAPK("prefix", variant.productFlavors[0].name, variant.name.capitalize())
                        outputFileName = createCopyTaskBundle("prefix", variant.productFlavors[0].name, variant.name.capitalize())
                }
            }
        }

Task for bundle:

ext.createCopyTaskBundle = { productName, variantName, taskSuffix ->
    def moveBundleTask = tasks.create(name: "move${taskSuffix}", type: Exec) {
        def input = new File("$projectDir\\$variantName\\release\\" + productName + "-" + variantName + "-release.aab")
        def folder = new File("$projectDir/$variantName")
        commandLine "copy.bat", input.path, "$projectDir\\apks\\${variantName}.aab", folder.path
        workingDir = rootProject.getProjectDir().getAbsolutePath()
        ignoreExitValue true
        dependsOn "bundle$taskSuffix"
    }
    tasks["bundle$taskSuffix"].finalizedBy = [moveBundleTask]

    return productName + "-" + variantName + "-release.aab"
}

Task for APK:

ext.createCopyTaskAPK = { productName, variantName, taskSuffix ->
    def moveAPKTask = tasks.create(name: "apk${taskSuffix}", type: Exec) {
        def input = new File("$projectDir\\$variantName\\release\\" + productName + "-" + variantName + "-release.apk")
        def folder = new File("$projectDir/$variantName")
        commandLine "copy.bat", input.path, "$projectDir\\bundles\\${variantName}.apk", folder.path
        workingDir = rootProject.getProjectDir().getAbsolutePath()
        ignoreExitValue true
        dependsOn "assemble$taskSuffix"
    }
    tasks["assemble$taskSuffix"].finalizedBy = [moveAPKTask]

    return productName + "-" + variantName + "-release.apk"
}

Content of copy.bat for windows:

ECHO COPYING %1 to %2
COPY %1 %2
3c71
  • 4,313
  • 31
  • 43
-3

You have to set different flavors if you want to have different output apks.

gaomode
  • 156
  • 1
  • 11