在使用Android Studio开发任何Android应用时,我们会在创建整个Android应用后生成APK文件。我们使用此.apk文件将其发布到我们的Google Play控制台或任何其他平台上。当我们生成用于发布apk的.apk文件时。我们总是看到我们的apk文件名称以release.apk文件开头。在本文中,我们将看到5种不同的方法来更改发行版apk的apk名称。
- 方法1:更新build.gradle文件以更改APK文件名
- 方法2:更新build.gradle文件以更改APK文件名
- 方法3:更新用于调试apk的apk名称
- 方法4:更新发行版apk的apk名称
- 方法5:生成APK文件后重命名APK名称
Prerequisite: For using this method to rename your apk file you should be having an Android Studio project ready with you. In this, we will be updating our apk name and generate a new APK with a different name.
方法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文件。