0

My AndroidStudio project only generates app-release.apk files via the menu "Build > Generate signed APK...". I found out how to change 'app' to match my app's name (here: Why is my APK name generic? ) but I still want some kind of app-release-X.apk (where X is the actual version code of that APK). I read that it is possible to modify the output path here https://stackoverflow.com/a/27678564/3410474 but couldn't find a way to use my version code in there.

If it is also possible to additionally use the version name (like 1.0) that would be even better, but the version code is more important to me.

Community
  • 1
  • 1
Valentin Kuhn
  • 753
  • 9
  • 25

1 Answers1

0

Inside your gradle properties you can define variables

APPLICATION_NAME=Nome
APPLICATION_VERSION_MAJOR=0
APPLICATION_VERSION_MINOR=1
APPLICATION_VERSION_PATCH=0
APPLICATION_VERSION_CODE=10

then in your gradle you can grab those variables and use them to rename the name

def appName = project.APPLICATION_NAME
def versionMajor = Integer.parseInt(project.APPLICATION_VERSION_MAJOR)
def versionMinor = Integer.parseInt(project.APPLICATION_VERSION_MINOR)
def versionPatch = Integer.parseInt(project.APPLICATION_VERSION_PATCH)
def versionCode = Integer.parseInt(project.APPLICATION_VERSION_CODE)

android {
   ....

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def separator = "_"
            def buildType = variant.variantData.variantConfiguration.buildType.name

            def newApkName = appName + separator + versionMajor + separator + versionMinor + separator + versionPatch + separator + versionCode + ".apk"
            output.outputFile = new File(output.outputFile.parent, newApkName)
        }
    }
}

you can play with the vars and choose a name type that works for you

xanexpt
  • 713
  • 8
  • 20