Bangaram
Bangaram

Reputation: 95

Generate apk name as package name in Android Studio

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.

Upvotes: 6

Views: 9915

Answers (5)

Upendra Shah
Upendra Shah

Reputation: 2301

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)
        }
    }
}

Upvotes: 0

John smith
John smith

Reputation: 1805

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:)

Upvotes: 2

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364968

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" );

Upvotes: 16

Andy.Zhao
Andy.Zhao

Reputation: 258

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"

Upvotes: 0

slezadav
slezadav

Reputation: 6141

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)
    }
}

Upvotes: 4

Related Questions