android figure in front of macbook pro

How to Change APK File Name in Android

Today, I want to share with you some valuable file naming scripts for gradle to change your artifact output file names.

Gradle is a build system which lets you to produce both android archives(.aar) and android packages(.apk) easily.

In android studio the gradle build settings will produce a very boring standart artifact name for you like output files by default.

android gradle artifact apk
Default gradle output filename for apk. {appname}-{flavorname}-{variantname}.apk

To change this default file naming for your artifacts you can use the below scripts:


//apply plugin: 'com.android.library' //uncomment this line for android libraries
//apply plugin: 'com.android.application' //uncomment this line for android applications
def renameArtifact(variant, defaultConfig) {
variant.outputs.each { output ->
def formattedDate = new Date().format('yyyyMMddHHmmss')
def fullName = output.outputFile.name
def projectName = fullName.substring(0, fullName.indexOf('-'))
output.outputFile = new File(
(String) output.outputFile.parent,
(String) output.outputFile.name.replace(projectName, "${projectName}-${defaultConfig.versionName}-${formattedDate}"))
}
}
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
//include this line for your android libraries
//libraryVariants.all { variant -> renameArtifact(variant, defaultConfig) }
//include this line for your android applications
//applicationVariants.all { variant -> renameArtifact(variant, defaultConfig) }
defaultConfig {
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//include your dependencies
}

view raw

build.gradle

hosted with ❤ by GitHub

For your android library project you have to uncomment line 21.

For your android applications you have to uncomment line 21.

After making these changes, the output will be like that:

android artifact apk
The artifact name is changed after modifying the build.gradle file.

As you can see, the artifact output file name contains appname, current version, timestamp, flavor name, variant name.

If you have any questions, please ask in the comments below.

Thank you.

Related posts


Posted

in

by

Comments

One response to “How to Change APK File Name in Android”

  1. TechToDown Avatar
    TechToDown

    Thank you for sharing this helpful guide on changing the APK file name in Android using Gradle. It’s a great way to customize the artifact naming for Android applications and libraries. The step-by-step instructions and explanations are clear and easy to follow.

Leave a Reply

Your email address will not be published. Required fields are marked *