Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change apk name with Gradle

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?

like image 846
Grzegorz Avatar asked Mar 02 '14 10:03

Grzegorz


People also ask

What is APK stand for?

An APK (Android Package Kit) is the file format for applications used on the Android operating system. APK files are compiled with Android Studio, which is the official integrated development environment (IDE) for building Android software. An APK file includes all of the software program's code and assets.


1 Answers

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)     } } 
like image 103
Leszek Avatar answered Oct 10 '22 02:10

Leszek