📌  相关文章
📜  如何在Android Studio中更改默认生成的apk名称?

📅  最后修改于: 2021-05-10 14:54:54             🧑  作者: Mango

在使用Android Studio开发任何Android应用时,我们会在创建整个Android应用后生成APK文件。我们使用此.apk文件将其发布到我们的Google Play控制台或任何其他平台上。当我们生成用于发布apk的.apk文件时。我们总是看到我们的apk文件名称以release.apk文件开头。在本文中,我们将看到5种不同的方法来更改发行版apk的apk名称。

  1. 方法1:更新build.gradle文件以更改APK文件名
  2. 方法2:更新build.gradle文件以更改APK文件名
  3. 方法3:更新用于调试apk的apk名称
  4. 方法4:更新发行版apk的apk名称
  5. 方法5:生成APK文件后重命名APK名称

方法1:更新build.gradle文件以更改APK文件名

在这种方法中,我们将在build.gradle文件中添加代码以更改.apk文件的名称。在这个过程中。导航至应用程序> Gradle脚本> build.gradle文件,然后在发布块下方的buildTypes部分中添加以下代码。下面给出了构建类型部分的代码。在代码中添加了注释,以便更详细地了解。

XML
buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        // add the code from below line. 
        applicationVariants.all{
            // this methid is use to rename your all apk weather
            // it may be signed or unsigned(debug apk)
            variant ->
                variant.outputs.each{
                    // on below line we are setting a 
                    // name to our apk as GFG.apk
                    output->
                        def name = "GFG.apk"
                        // on below line we are setting the 
                        // outputFile Name to our apk file. 
                        output.outputFileName = name
                }
       }
}


XML
buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        // add the code from below line
        applicationVariants.all{
            // this method is use to rename your all apk weather 
            // it may be signed or unsigned(debug apk)
            variant ->
                variant.outputs.each{
                    // on below line we are setting 
                    // a name to our apk as GFG.apk
                    output->
                        // on below line we are adding version name to 
                        // our .apk file along with the app name
                        def name = "GFG(${variant.versionName}).apk"
                        // on below line we are setting the 
                        // outputFile Name to our apk file
                        output.outputFileName = name
                }
        }
}


XML
buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        // add the code from below line.
        applicationVariants.all{
            // this method is use to rename your all apk weather
            // it may be signed or unsigned(debug apk)
            variant ->
                variant.outputs.each{
                    // on below line we are setting a name to our apk
                    output->
                        // on below line we are specifying our app name.
                        project.ext { appName = 'GFG' }
                        // on below line we are adding the formatted date to our apk file name.
                        def formattedDate = new Date().format('yyyy-MM-dd-HH-mm-ss')
                        // on below line we are creating a new name for our apk.
                        def newName = output.outputFile.name
                        // on below line we are replacing our previous name with our app name.
                        newName = newName.replace("app-", "$project.ext.appName-")
                        // on below line we are replacing -debug with our formatted date.
                        newName = newName.replace("-debug", "-debug-" + formattedDate)
                        // at last we are setting our apk name to it. 
                        output.outputFileName  = newName
                }
        }
}


XML
buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        // add the code from below line.
        applicationVariants.all{
            // this method is use to rename your release apk only
            variant ->
                variant.outputs.each{
                    // on below line we are setting a name to our apk
                    output->
                        // on below line we are specifying our app name.
                        project.ext { appName = 'GFG' }
                        // on below line we are adding the formatted date to our apk file name.
                        def formattedDate = new Date().format('yyyy-MM-dd-HH-mm-ss')
                        // on below line we are creating a new name for our apk.
                        def newName = output.outputFile.name
                        // on below line we are replacing our previous name with our app name.
                        newName = newName.replace("app-", "$project.ext.appName-")
                        // on below line we are replacing -release with our formatted date.
                        newName = newName.replace("-release", "-release-" + formattedDate)
                        // at last we are setting our apk name to it.
                        output.outputFileName  = newName
                }
        }
}


在build.gradle文件中添加此代码之后。现在,单击立即同步选项以同步您的项目,然后构建您的APK。您将看到您的APK名称以GFG.apk命名。

方法2:更新build.gradle文件以更改APK文件名

在这种方法中,我们将在build.gradle文件中添加一个简单的代码段,在其中我们将使用版本代码更新apk名称。在这个过程中。导航至应用程序> Gradle脚本> build.gradle文件,然后在发布块下方的buildTypes部分中添加以下代码。下面给出了构建类型部分的代码。在代码中添加了注释,以便更详细地了解。在下面的过程中,生成的apk名称将为GFG(1).apk

XML格式

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        // add the code from below line
        applicationVariants.all{
            // this method is use to rename your all apk weather 
            // it may be signed or unsigned(debug apk)
            variant ->
                variant.outputs.each{
                    // on below line we are setting 
                    // a name to our apk as GFG.apk
                    output->
                        // on below line we are adding version name to 
                        // our .apk file along with the app name
                        def name = "GFG(${variant.versionName}).apk"
                        // on below line we are setting the 
                        // outputFile Name to our apk file
                        output.outputFileName = name
                }
        }
}

方法3:更新用于调试apk的apk名称

在这种方法中,我们将添加一个代码段,使用该代码段我们可以仅为调试apk更新apk名称。在此过程中,我们必须导航至应用程序> Gradle脚本> build.gradle文件,并在发布块下方的buildTypes部分中添加以下代码。下面给出了构建类型部分的代码。在代码中添加了注释,以便更详细地了解。在以下过程中,生成的apk名称将为GFG-debug-2021-03-10-13-07-18.apk

XML格式

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        // add the code from below line.
        applicationVariants.all{
            // this method is use to rename your all apk weather
            // it may be signed or unsigned(debug apk)
            variant ->
                variant.outputs.each{
                    // on below line we are setting a name to our apk
                    output->
                        // on below line we are specifying our app name.
                        project.ext { appName = 'GFG' }
                        // on below line we are adding the formatted date to our apk file name.
                        def formattedDate = new Date().format('yyyy-MM-dd-HH-mm-ss')
                        // on below line we are creating a new name for our apk.
                        def newName = output.outputFile.name
                        // on below line we are replacing our previous name with our app name.
                        newName = newName.replace("app-", "$project.ext.appName-")
                        // on below line we are replacing -debug with our formatted date.
                        newName = newName.replace("-debug", "-debug-" + formattedDate)
                        // at last we are setting our apk name to it. 
                        output.outputFileName  = newName
                }
        }
}

方法4:更新发行版apk的apk名称

在这种方法中,我们将添加一个代码段,使用该代码段我们可以仅更新发行版apk的apk名称。在此过程中,我们必须导航至应用程序> Gradle脚本> build.gradle文件,并在发布块下方的buildTypes部分中添加以下代码。下面给出了构建类型部分的代码。在代码中添加了注释,以便更详细地了解。在以下过程中,生成的apk名称将为GFG-release-2021-03-10-13-07-18.apk

XML格式

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        // add the code from below line.
        applicationVariants.all{
            // this method is use to rename your release apk only
            variant ->
                variant.outputs.each{
                    // on below line we are setting a name to our apk
                    output->
                        // on below line we are specifying our app name.
                        project.ext { appName = 'GFG' }
                        // on below line we are adding the formatted date to our apk file name.
                        def formattedDate = new Date().format('yyyy-MM-dd-HH-mm-ss')
                        // on below line we are creating a new name for our apk.
                        def newName = output.outputFile.name
                        // on below line we are replacing our previous name with our app name.
                        newName = newName.replace("app-", "$project.ext.appName-")
                        // on below line we are replacing -release with our formatted date.
                        newName = newName.replace("-release", "-release-" + formattedDate)
                        // at last we are setting our apk name to it.
                        output.outputFileName  = newName
                }
        }
}

方法5:生成APK文件后重命名APK名称

通过这种方法,我们将在生成APK文件后更新APK名称。在这种方法中,我们只需使用Build选项中Top Action栏中的Generate Signed APK选项来生成.apk文件。生成您的.apk文件之后。导航到apk所在的文件夹。在该文件夹中,选择您的APK文件。右键单击它,然后单击重命名选项以重命名您的APK文件。

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!