Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set versionName in APK filename using gradle?

I'm trying to set a specific version number in the gradle auto-generated APK filename.

Now gradle generates myapp-release.apk but I want it to look something like myapp-release-1.0.apk.

I have tried renaming options that seems messy. Is there a simple way to do this?

buildTypes {     release {        signingConfig signingConfigs.release        applicationVariants.each { variant ->        def file = variant.outputFile        variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" +    defaultConfig.versionName + ".apk"))     } } 

I have tried the code above with no luck. Any suggestions? (using gradle 1.6)

like image 458
codaR0y Avatar asked Aug 20 '13 10:08

codaR0y


People also ask

Which is the app level Gradle file?

gradle file, located in the root project directory, defines dependencies that apply to all modules in your project. By default, the top-level build file uses the plugins block to define the Gradle dependencies that are common to all modules in the project.

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.


1 Answers

I only have to change the version name in one place. The code is simple too.

The examples below will create apk files named named MyCompany-MyAppName-1.4.8-debug.apk or MyCompany-MyAppName-1.4.8-release.apk depending on the build variant selected.

Note that this solution works on both APK and App Bundles (.aab files).

See Also: How to change the proguard mapping file name in gradle for Android project

#Solution for Recent Gradle Plugin

android {     compileSdkVersion 22     buildToolsVersion "22.0.1"     defaultConfig {         applicationId "com.company.app"         minSdkVersion 13         targetSdkVersion 21         versionCode 14       // increment with every release         versionName '1.4.8'   // change with every release         setProperty("archivesBaseName", "MyCompany-MyAppName-$versionName")     } } 

The above solution has been tested with the following Android Gradle Plugin Versions:

  • 3.6.4 (August 2020)
  • 3.5.2 (November 2019)
  • 3.3.0 (January 2019)
  • 3.1.0 (March 2018)
  • 3.0.1 (November 2017)
  • 3.0.0 (October 2017)
  • 2.3.2 (May 2017)
  • 2.3.1 (April 2017)
  • 2.3.0 (February 2017)
  • 2.2.3 (December 2016)
  • 2.2.2
  • 2.2.0 (September 2016)
  • 2.1.3 (August 2016)
  • 2.1.2
  • 2.0.0 (April 2016)
  • 1.5.0 (2015/11/12)
  • 1.4.0-beta6 (2015/10/05)
  • 1.3.1 (2015/08/11)

I'll update this post as new versions come out.

#Solution Tested Only on versions 1.1.3-1.3.0 The following solution has been tested with the following Android Gradle Plugin Versions:

  • 1.3.0 (2015/07/30) - Not Working, bug scheduled to be fixed in 1.3.1
  • 1.2.3 (2015/07/21)
  • 1.2.2 (2015/04/28)
  • 1.2.1 (2015/04/27)
  • 1.2.0 (2015/04/26)
  • 1.2.0-beta1 (2015/03/25)
  • 1.1.3 (2015/03/06)

app gradle file:

apply plugin: 'com.android.application'  android {     compileSdkVersion 21     buildToolsVersion "21.1.2"     defaultConfig {         applicationId "com.company.app"         minSdkVersion 13         targetSdkVersion 21         versionCode 14       // increment with every release         versionName '1.4.8'   // change with every release         archivesBaseName = "MyCompany-MyAppName-$versionName"     } } 
like image 135
Jon Avatar answered Oct 21 '22 10:10

Jon