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.
To change this default file naming for your artifacts you can use the below scripts:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 | |
} |
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:
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.
Leave a Reply