Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change apk name by using gradle like this format?

Tags:

android

gradle

I'd like to change "app-release.apk" file name to like following when I build an app by using gradle.

[format]
(appname of package name)_V(version code)_(yyMMdd)_(R|T)

[explain]
(appname of package name) : example) com.example.myApp -> myApp
(version code) : build version code 2.2.3 -> 223
(yyMMdd) : build date 2015.11.18 -> 151118  
(R|T) : if app is release, "R" but debug is "T".

If I generate an apk file in release, result is : myApp_V223_151118_R.apk.

How to make a file name like this in gradle?

like image 566
S.J. Lim Avatar asked Nov 18 '15 08:11

S.J. Lim


People also ask

How do I rename an APK file?

Navigate to the folder in which your apk is present. In that folder select your APK file. Right-click on it and click on rename option to rename your APK file.

How do I change my build gradle app name?

You can use strings. xml in different folders, see Android separate string values for release and debug builds. Then paste it to app\src\debug\res\values\ and app\src\release\res\values\ folders. Replace "Your app name" in debug and release files.

Can I rename app debug APK?

If anyone is looking to change apk name outside the Android studio(just to send this file to someone else, as in my case), just right click the app name and change it to whatever you want.

What is root project in gradle?

A multi-project build in Gradle consists of one root project, and one or more subprojects. A basic multi-project build contains a root project and a single subproject. This is a structure of a multi-project build that contains a single subproject called app : Example 1. Basic multi-project build.


3 Answers

This may be the shortest way:

defaultConfig {     ...     applicationId "com.blahblah.example"     versionCode 1     versionName "1.0"     setProperty("archivesBaseName", applicationId + "-v" + versionCode + "(" + versionName + ")") } 

buildType: like so

buildTypes {     debug {         ...         versionNameSuffix "-T"     }     release {         ...         versionNameSuffix "-R"     } } 

Keep in mind, Android Studio adds versionNameSuffix by build type name by default, so you may not need this.

Upd. In new versions of Android Studio you can it write little shorter(thanks for szx comment):

defaultConfig {     ...     archivesBaseName = "$applicationId-v$versionCode($versionName)" } 
like image 76
Anrimian Avatar answered Sep 25 '22 13:09

Anrimian


Update: Please check Anrimian's answer below which is much simpler and shorter.

Try this:

gradle.properties

applicationName = MyApp 

build.gradle

android {   ...   defaultConfig {      versionCode 111      ...   }   buildTypes {      release {          ...          applicationVariants.all { variant ->              renameAPK(variant, defaultConfig, 'R')          }      }      debug {          ...          applicationVariants.all { variant ->              renameAPK(variant, defaultConfig, 'T')          }      }   } } def renameAPK(variant, defaultConfig, buildType) {  variant.outputs.each { output ->      def formattedDate = new Date().format('yyMMdd')       def file = output.packageApplication.outputFile      def fileName = applicationName + "_V" + defaultConfig.versionCode + "_" + formattedDate + "_" + buildType + ".apk"      output.packageApplication.outputFile = new File(file.parent, fileName)  } } 

Reference: https://stackoverflow.com/a/30332234/206292 https://stackoverflow.com/a/27104634/206292

like image 33
Krishnaraj Avatar answered Sep 23 '22 13:09

Krishnaraj


2019 / 2020 - How to change APK name For Gradle 3.3, 3.4, 3.5, 4.0 and above

android {
   ......
   applicationVariants.all { variant ->
       variant.outputs.all {
           def flavor = variant.name
           def versionName = variant.versionName
           outputFileName = "prefix_${flavor}_${versionName}.apk"
       }
   }
}

The result would be like this,

prefix_release_1.0.1.apk

like image 43
Zumry Mohamed Avatar answered Sep 24 '22 13:09

Zumry Mohamed