pravingaikwad07
pravingaikwad07

Reputation: 522

How to generate output APK file with Customised name in Android Studio?

I want to generate output apk file using customized name. Eg. By default, android studio generates "app-debug.apk" file. But I want it to be - "MyAppName_myCurrentProdFlavour_vMyVersionName.apk" How to do this using build.gradle file of module

Upvotes: 8

Views: 6093

Answers (5)

Ashvin solanki
Ashvin solanki

Reputation: 4809

APK name with version and date and time.

defaultConfig {
    def date = new Date()
    def formattedDate = date.format('ddMMYYYY')
    def formattedVersion = versionName
    formattedVersion = formattedVersion.replaceAll('\\.','')
    setProperty("archivesBaseName", "ApplicationName_"+formattedVersion+"_"+formattedDate)
}

Upvotes: 2

pravingaikwad07
pravingaikwad07

Reputation: 522

1. For changing app bundle name (.aab file):

in app module's build.gradle:

def dateFormat = new Date().format('ddMMMyy_HHmm')
defaultConfig {
        applicationId "com.xyz"
        minSdk 21
        targetSdk 31
        versionCode 3
        versionName "1.0.2"

        setProperty("archivesBaseName", "yourappname" + "_v" + versionName + "(" + versionCode + ")_"+dateFormat)

    }

For example: if your app name is XYZ then the bundle name will generated with name: XYZ_v1.0.2(3)_28Jan22_1152-prod-release

where, yourappname is XYZ , versionName is 1.0.2 , versionCode is 3 , dateFormat is 28Jan22_1152 , and app flavorType and buildType is prod-release

2. For changing apk file name (.apk file)

in app module's build.gradle:

android {

applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def name = "XYZ"
                def SEP = "_"
                def flavor = variant.productFlavors[0].name
                def buildType = variant.buildType.name
                def versionName = defaultConfig.versionName
                def versionCode = defaultConfig.versionCode
                def newApkName = name + SEP + "v" + versionName + SEP + flavor + SEP + buildType + SEP + "vc" + versionCode + SEP + dateFormat + ".apk"
                output.outputFileName = newApkName
            }
        }    
}

For example, if your app name is XYZ then this will generate APK file with following custom name: XYZ_v1.0.2_prod_release_vc3_28Jan22_1152


Note: You can change dateFormat as per your requirement, for apk name if you don't have productFlavours then remove all related references from the code given above.

Upvotes: 5

Bunny
Bunny

Reputation: 1064

You can customize apk file name by using this:

Go to app Gradle

  setProperty("archivesBaseName", "yourapp-packagename-$versionName")

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "com.test.app"
        minSdkVersion 13
        targetSdkVersion 21
        versionCode 14       // increment with every release
        versionName '1.4.8'   // change with every release
        setProperty("archivesBaseName", "yourapp-packagename-$versionName")
    }

}

Upvotes: 3

Khemraj Sharma
Khemraj Sharma

Reputation: 59004

Easiest way

Before app_debug.apk

After com.pcvark.jobtest-v1(1.0)-debug.apk

android {
    ..
    defaultConfig {
        ...
        setProperty("archivesBaseName", applicationId + "-v" + versionCode + "(" + versionName + ")")
    }

Tip You can also set versionNameSuffix for different buildType and variant (if you have). This will make different named apk for both debug and release.

buildTypes {
        debug {
          versionNameSuffix "-T"
        }
        release {
          versionNameSuffix "-R"
        }
    }

Further reading

https://stackoverflow.com/a/20660274/6891563

https://docs.gradle.org/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html

Upvotes: 9

Mohammed Rampurawala
Mohammed Rampurawala

Reputation: 3112

You can use the following mechanism as seperately for both debug and release build which will allow you to provide different names for each build.

buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    project.ext { appName = 'MY-CUSTOM-APP' }
                    def newName = output.outputFile.name
                    newName = newName.replace("app-", "$project.ext.appName-")
                    output.outputFile = new File(output.outputFile.parent, newName)
                }
            }
            signingConfig signingConfigs.MyApp
        }
        release {
            debuggable false
            jniDebuggable false
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    project.ext { appName = 'MY-CUSTOM-APP' }
                    def newName = output.outputFile.name
                    newName = newName.replace("app-", "$project.ext.appName-")
                    output.outputFile = new File(output.outputFile.parent, newName)
                }
            }
            signingConfig signingConfigs.MyApp
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

Happy to help!!!

Upvotes: 2

Related Questions