I have an Android project which uses Gradle for build process. Now I want to add two extra build types staging and production, so my build.gradle contains:
android {
buildTypes {
release {
runProguard false
proguardFile getDefaultProguardFile('proguard-android.txt')
}
staging {
signingConfig signingConfigs.staging
applicationVariants.all { variant ->
appendVersionNameVersionCode(variant, defaultConfig)
}
}
production {
signingConfig signingConfigs.production
}
}
}
and my appndVersionNameVersionCode looks like:
def appendVersionNameVersionCode(variant, defaultConfig) {
if(variant.zipAlign) {
def file = variant.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
variant.outputFile = new File(file.parent, fileName)
}
def file = variant.packageApplication.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
variant.packageApplication.outputFile = new File(file.parent, fileName)
}
If I execute task assembleStaging then I get proper name of my apk, but when I execute assembleProduction then I get changed names of my apk (like in staging case). For example:
MyApp-defaultFlavor-production-9.9.9-999.apk
MyApp-defaultFlavor-production-9.9.9-999.apk
It looks like in production build type is executed appendVersionNameVersionCode. How can I avoid it?
As CommonsWare wrote in his comment, you should call appendVersionNameVersionCode
only for staging variants. You can easily do that, just slightly modify your appendVersionNameVersionCode
method, for example:
def appendVersionNameVersionCode(variant, defaultConfig) {
//check if staging variant
if(variant.name == android.buildTypes.staging.name){
if(variant.zipAlign) {
def file = variant.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
variant.outputFile = new File(file.parent, fileName)
}
def file = variant.packageApplication.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
variant.packageApplication.outputFile = new File(file.parent, fileName)
}
}
Lecho's solution doesn't work for Android Gradle Plugin 0.14.3+ because of removal of deprecated APIS:
http://tools.android.com/tech-docs/new-build-system
Almost 1.0: removed deprecated properties/methods
...
- Variant.packageApplication/zipAlign/createZipAlignTask/outputFile/processResources/processManifest (use the variant output)
The following works for me:
def appendVersionNameVersionCode(variant, defaultConfig) {
variant.outputs.each { output ->
if (output.zipAlign) {
def file = output.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
output.outputFile = new File(file.parent, fileName)
}
def file = output.packageApplication.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
output.packageApplication.outputFile = new File(file.parent, fileName)
}
}
I have use little generic version of it. build and install fine.
for example your projectName is "Salad" then for staging apk name will be "Salad-staging-dd-MM-YY" you can also change for debug and release apk. hope my solution will be better.
Change in projectName/app/build.gradle
buildTypes {
debug{
}
staging{
debuggable true
signingConfig signingConfigs.debug
applicationVariants.all { variant ->
variant.outputs.each { output ->
def date = new Date();
def formattedDate = date.format('dd-MM-yyyy')
def appName = getProjectDir().getParentFile().name
output.outputFile = new File(output.outputFile.parent,
output.outputFile.name.replace(getProjectDir().name +"-staging", "$appName-staging-" + formattedDate)
//for Debug use output.outputFile = new File(output.outputFile.parent,
// output.outputFile.name.replace("-debug", "-" + formattedDate)
)
}
}
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
to change Android app name ref
Here is what I have done:
def appName
if (project.hasProperty("applicationName")) {
appName = applicationName
} else {
appName = parent.name
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace("app-release.apk", appName + "_V" + versionCode + ".apk"))
}
}