7

Android studio will generate default apk name as app-(release|debug).apk.
How to generate apk file name same as package name of the app like com.example-debug.apk.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Bangaram
  • 105
  • 1
  • 6

5 Answers5

16

You can do it without using another tasks, setting the archivesBaseName.

For example:

 defaultConfig {
      ....
      project.ext.set("archivesBaseName", "MyName-" + defaultConfig.versionName);

  }

Output:

MyName-1.0.12-release.apk

In your case:

project.ext.set("archivesBaseName", "com.example" );
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 1
    You might be able to access rootProject name and use it like this: `project.ext.set("archivesBaseName", rootProject.getName() + "-" + defaultConfig.versionName);` – Garcia Sylvain Apr 03 '17 at 12:36
  • unfortunately doesn't scale when you work with variants as archivesBaseName is global for all of them – Ewoks Jun 01 '17 at 07:08
4

Try putting this in your module's build.gradle

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def file = output.outputFile
        def appId = android.defaultConfig.applicationId
        def fileName = appId + "-" variant.buildType.name +".apk"
        output.outputFile = new File(file.parent, fileName)
    }
}
slezadav
  • 6,104
  • 7
  • 40
  • 61
  • This only spits out one APK for me and doesn't spit out an APK for each build variant. – Subby Jul 03 '16 at 23:44
  • Of course not, if you want apk for another variant, you have to change the build variant first. Or use a command to build all versions. This code only changes the name of the apk, nothing more. – slezadav Jul 04 '16 at 06:11
  • using './gradlew assemble' in terminal should spit out all buildTypes of all variants. Otherwise you can do './gradlew assembleRelease' or './gradlew assembleDebug' for these specific buildTypes – Ewoks Jun 01 '17 at 07:11
2

you can see this link. or Illogical option to rename your release|debug.apk with name what you want in file browser.

this code may be useful for you:

buildTypes {
release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def formattedDate = new Date().format('yyyyMMddHHmmss')
            def newName = output.outputFile.name
            newName = newName.replace("app-", "$rootProject.ext.appName-") //"MyAppName" -> I set my app variables in the root project
            newName = newName.replace("-release", "-release" + formattedDate)
            //noinspection GroovyAssignabilityCheck
            output.outputFile = new File(output.outputFile.parent, newName)
        }
    }
}
    debug {
    }
}

enjoy your code:)

Community
  • 1
  • 1
John smith
  • 1,781
  • 17
  • 27
0

Create a file named customname.gradle in the top level directory of the project.Put this code into it.

android.applicationVariants.all { variant ->;
def appName
//Check if an applicationName property is supplied; if not use the name of the parent project.
if (project.hasProperty("applicationName")) {
    appName = applicationName
} else {
    appName = parent.name
}

variant.outputs.each { output ->;
    def newApkName
    //If there's no ZipAlign task it means that our artifact will be unaligned and we need to mark it as such.
    if (output.zipAlign) {
        newApkName = "${appName}-${output.baseName}-${variant.versionName}.apk"
    } else {
        newApkName = "${appName}-${output.baseName}-${variant.versionName}-unaligned.apk"
    }
    output.outputFile = new File(output.outputFile.parent, newApkName)
}}

Then in your app module's gradle add this code

apply from: "../customname.gradle"
Andy.Zhao
  • 250
  • 3
  • 16
0

This may help you. This code will create app name like applicationId-release.apk or applicationId-debug.apk in which applicationId can be your package name.

buildTypes {

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def newName = output.outputFile.name
            newName = newName.replace("app-", applicationId)
            output.outputFile = new File(output.outputFile.parent, newName)
        }
    }
}
Upendra Shah
  • 2,218
  • 17
  • 27