Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 244
Mihir Trivedi Avatar asked Jan 08 '20 17:01

Mihir Trivedi


3 Answers

As per This answer, you can go with Option - Two with minor changes as mention below only works for APK, not the 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"
    }
}

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

like image 135
Mihir Trivedi Avatar answered Oct 18 '22 20:10

Mihir Trivedi


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"
    }
}
like image 39
Mir Milad Hosseiny Avatar answered Oct 18 '22 21:10

Mir Milad Hosseiny


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
}
like image 43
Brijesh Chavda Avatar answered Oct 18 '22 22:10

Brijesh Chavda