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

问题描述 投票:13回答:9

默认情况下,IDE会像app-debug.apkapp-release.apk文件一样生成apk,但我需要生成App的Apk的特定名称。

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

谢谢,

android android-studio gradle apk
9个回答
9
投票

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

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

setting.gradle文件包含

include ':app'

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

对于示例应用程序,在包含':app'中由iPlanter替换它,如下所示

include ':iPlanter'

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


6
投票

只需添加

   archivesBaseName = "NAME_YOU_WANT"

在你的gradle文件的android {}部分。

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


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

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

1
投票

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

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

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

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”+ versionName

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
}

0
投票

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


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

}

© www.soinside.com 2019 - 2024. All rights reserved.