Change generated apk name from “app-debug.apk”

2019-02-11 15:02发布

问题:

Every time I run a project with Android Studio (1.02) it's generate an unsigned apk which located in ..\build\outputs\apk folder and get by default the name "app-debug.apk"

I want to change that default name to something else.

Is it possible? How?

回答1:

You can use applicationVariants and change the output file in the build.gradle. You can also modify the name regarding to your needs.

buildTypes{

    applicationVariants.all { variant ->                              
        variant.outputs.each { output ->                              
            output.outputFile = file("$project.buildDir/apk/test.apk")
        }                                                             
    }

}


回答2:

In build.gradle (Module: app):

android {
    ...
    defaultConfig {
        ...
        setProperty("archivesBaseName", "MyNewAppNameGoesHere")
    }
}

This works by modifying the archivesBaseName-property and works for Gradle Version >= 2.0, last tested with 2.1.0.



回答3:

Solution for gradle3:

android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = applicationId;
        outputFileName += "-v" + android.defaultConfig.versionName;
        if (variant.buildType.name == "release") {
            outputFileName += ".apk";
        } else {
            outputFileName += "-SNAPSHOT.apk";
        }
    }
}


回答4:

The 'app' part of the name uses the folder name of your application's module (default is 'app'). See the link below on how to change that.
Why is my APK name generic?

The '-debug' prefix is something you can change in the Module settings at Build Types



回答5:

If anyone is looking to change apk name outside the Android studio(just to send this file to someone else, as in my case), just right click the app name and change it to whatever you want.



回答6:

defaultConfig {  

    applicationId "com.dcarl661.awesomelayout"

    minSdkVersion 14

    targetSdkVersion 25

    versionCode 1

    versionName "1.0"

    //here put it in the section where you have the version stuff
    setProperty("archivesBaseName", "AwesomeLayout")

}


回答7:

Maybe this question (and its accepted answer) are interesting for you: How to set versionName in APK filename using gradle? It describes how to put your versionName into the APK name.

You have to use the 'Update' part of the accepted answer. Also you need the versionName (in this example) declared in your defaultConfig in the build.gradle file. It does also work if you define versionCode in the build.gradle (as described here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Manifest-entries ).