Android:如何在Android Studio中更改生成的apk文件的特定名称?

时间:2016-09-23 07:04:02

标签: android android-studio gradle apk

默认情况下,IDE会生成类似app-debug.apkapp-release.apk文件的apk,但我需要生成应用的 Apk 的特定名称。

例如: 我的应用程序名称是 iPlanter ,因此我需要生成 iPlanter-debug.apk iPlanter-release.apk 而不是app-debug.apk或分别为app-release.apk

谢谢,

9 个答案:

答案 0 :(得分:9)

第1步: 转到主项目 root ,在应用下,右键单击应用并将应用重构为特定名称(示例iPlanter )并按确定

第2步: 转到项目设置文件 setting.gradle文件

setting.gradle文件包含

include ':app'

现在需要按特定名称替换app。

例如 应用程序由 include':app' 中的iPlanter替换 它看起来像下面

include ':iPlanter'

然后同步项目,之后运行您的应用程序。 最后,App会生成一个类似 iPlanter-debug.apk iPlanter-release.apk 文件的apk。

答案 1 :(得分:5)

添加

   archivesBaseName = "NAME_YOU_WANT"

在gradle文件的android {}部分。

您将获得“NAME_YOU_WANT-release.apk”作为生成文件的名称。

答案 2 :(得分:4)

您可以将此用于包含当前日期和版本的应用名称

android {
def version = "2.4";
def milestone = "1";
def build = "0";
def name = getDate()+"APP NAME WHAT YOU WANT"+"v"+version


signingConfigs {
    config {
       ….
   }
}
compileSdkVersion Integer.parseInt(COMPILE_SDK)
buildToolsVersion BUILD_TOOLS_VERSION
defaultConfig {
   applicationId "com.PACKAGENAME"
   minSdkVersion Integer.parseInt(MIN_SDK_LIBRARY)
   targetSdkVersion Integer.parseInt(TARGET_SDK)
   versionCode 11
   versionName "2.3"
   multiDexEnabled true
}
buildTypes {
   debug {
       applicationVariants.all { variant ->
           variant.outputs.each { output ->
               def apk = output.outputFile;
               def newName;
               newName = apk.name.replace("-" + variant.buildType.name, "")
                       .replace(project.name, name);
               newName = newName.replace("-", "-" + version + "-" + milestone +
                       "-" + build + "-");
               output.outputFile = new File(apk.parentFile, newName);
           }
       }
   }

答案 3 :(得分:1)

试试这段代码:

defaultConfig{
      applicationVariants.all { variant ->
                changeAPKName(variant, defaultConfig)
            }
}

def changeAPKName(variant, defaultConfig) {
    variant.outputs.each { output ->
        if (output.zipAlign) {
            def file = output.outputFile
            output.packageApplication.outputFile = new File(file.parent, "Your APK NAME")
        }
        def file = output.packageApplication.outputFile
        output.packageApplication.outputFile = new File(file.parent, "Your APK NAME")
    }
}

答案 4 :(得分:1)

对于Android Studio 3,这对我有用:

applicationVariants.all { variant ->
        variant.outputs.all { output ->
            outputFileName = new File("AppName-" + variant.versionName + ".apk");
        }
}

答案 5 :(得分:1)

对于Android Studio 3.1,这对我有用:

android {
     ...............
     ...............

     applicationVariants.all { variant ->
            changeAPKName(variant, defaultConfig)
        }

  compileOptions {
       sourceCompatibility JavaVersion.VERSION_1_8
       targetCompatibility JavaVersion.VERSION_1_8
       }

      .................................
      .................................
     }

def changeAPKName(variant, defaultConfig) {
    variant.outputs.all { output ->
        outputFileName = new File("xxxxx" + variant.versionName +".apk")
    }
}

答案 6 :(得分:1)

您只需要在应用程序级别gradle中添加以下代码即可。

  1. 仅用于名称
  

archivesBaseName =“ NAME_YOU_WANT”

 defaultConfig {

       applicationId "com.PACKAGENAME"

       minSdkVersion Integer.parseInt(MIN_SDK_LIBRARY)

       targetSdkVersion Integer.parseInt(TARGET_SDK)

       versionCode 11

       versionName "2.3"

       multiDexEnabled true

       archivesBaseName = "NAME_YOU_WANT"


    }
  1. 名称和版本
  

archivesBaseName =“ NAME_YOU_WANT” +版本名称

defaultConfig {

   applicationId "com.PACKAGENAME"

   minSdkVersion Integer.parseInt(MIN_SDK_LIBRARY)

   targetSdkVersion Integer.parseInt(TARGET_SDK)

   versionCode 11

   versionName "2.3"

   multiDexEnabled true

   archivesBaseName = "NAME_YOU_WANT" + versionName
}

答案 7 :(得分:0)

在我的电脑上,只需将(通过重构)应用重命名为所需名称 yourName 即可。之后,setting.grandle文件中的include ':app'更改为include ':yourName'。但是,在我的情况下,由于同步错误,我需要关闭/重新打开Android Studio。因此,获取 yourName-debug.apk yourName-release.apk 之类的 apk

答案 8 :(得分:0)

这会对你有所帮助。此代码将创建应用程序名称,如iPlanter-release.apk或iPlanter-debug.apk

buildTypes {
   applicationVariants.all { variant ->
    variant.outputs.each { output ->
        project.ext { appName = 'iPlanter' }
        def newName = output.outputFile.name
        newName = newName.replace("app-", "$project.ext.appName-")
        output.outputFile = new File(output.outputFile.parent, newName)
     }
  }

}