Generate app name with version code, name and flavour name – Android

After a build is shared to a team or group of people, sometimes a question arises that what was the version code/name of the build shared, or from which product flavour it was from. Developers can inspect these details using analyse apk option in Android Studio. But, the problem is with other member who do not have Android studio to inspect these details.

In order to resolve this issue, it is better to put these details with APK filename. In this way all the details can be verified just by looking at the filename.

To achieve this we can put this gradle function in application level build.gradle file.

android.applicationVariants.all { variant ->
    def appName
    // if applicationName property is set in gradle.properties
    if (project.hasProperty("applicationName")) {
        appName = applicationName
    } else {
    // if not use the name of the parent project
        appName = parent.name
    }
    appName = appName.replaceAll(" ", "_")
    def formattedDate = new Date().format('yyyy-MM-dd_HH:mm:ss')
    variant.outputs.all { output ->
        outputFileName = "${appName}-${output.baseName}-${variant.versionCode}" +
                "(${variant.versionName})-${formattedDate}.apk"
        println("outputFileName: " + outputFileName)
    }
}

(adsbygoogle = window.adsbygoogle || []).push({});

The output of adding this gradle code block will be like this, considering following details

  1. Application name is “App Name”
  2. You have two product flavours setup, i.e. dev and prod.
  3. Application version code is 16
  4. Application version name is 2.7.4 (-dev is setup as versionNameSuffix)
App_Name-dev-debug-16(2.7.4-dev)-2020-04-19_12:08:10.apk
App_Name-prod-debug-16(2.7.4)-2020-04-19_12:08:10.apk
App_Name-dev-release-16(2.7.4-dev)-2020-04-19_12:08:10.apk
App_Name-prod-release-16(2.7.4)-2020-04-19_12:08:10.apk