How to set versionName in APK filename using gradle?

gradle apk name
archivesbasename
how to change apk name in android
flutter build apk name
change generated apk name
gradle initwith
debuggable true gradle
android 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)

How to change apk name by using gradle like this format?, outputFile def fileName = applicationName + "_V" + defaultConfig.versionCode + "_" + formattedDate + "_" + buildType + ".apk" output. Set Android apk filename in Gradle Updated Jun 20, 2019 in Android Gradle. By default the Android gradle plugin creates app-debug.apk, app-release-unsigned.apk and app-release.apk apk files during the build process. Fortunately it’s easy to change the file name to suit your need.

This solved my problem: using applicationVariants.all instead of applicationVariants.each

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

So it seems this does not work with 0.14+ versions of android studio gradle plugin.

This does the trick (Reference from this question ) :

android {
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File(
                    output.outputFile.parent,
                    output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
        }
    }
}

Set Android apk filename in Gradle · Taneli Korri, Change Gradle settings and output apks with more descriptive filenames. By default the Android gradle plugin creates app-debug.apk, In addition to versionName, you can also use other variables set in the defaultConfig  The Gradle Code. We’re going to make a gradle function called getVersionInfo which takes one argument: a File object which points at an APK and returns a VersionInfo object we can use however we

Producing better named Android APKs with Gradle, Producing betting named Android APKs with the Gradle build plugin. This stems from the default and preferred project setup where the application code is contained in the “app” directory, Start by creating a file name artifacts.gradle in the top level directory of the project. Put versionName}.apk" output. With a few small changes to the app build.gradle file, the .apk name can be changed to <app name>-release-<version>.apk. The file that needs to be edited is the application build.gradle file. It is located in the app directory of your Android Studio project. First step is to set project.archivesBaseName to your application name.

To sum up, for those don't know how to import package in build.gradle(like me), use the following buildTypes,

buildTypes {
      release {
        signingConfig signingConfigs.release
        applicationVariants.all { variant ->
            def file = variant.outputFile
            def manifestParser = new com.android.builder.core.DefaultManifestParser()
            variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile) + ".apk")) 
        }
    }       
}

===== EDIT =====

If you set your versionCode and versionName in your build.gradle file like this:

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 19
    versionCode 1
    versionName "1.0.0"
}

You should set it like this:

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

====== EDIT with Android Studio 1.0 ======

If you are using Android Studio 1.0, you will get an error like this:

Error:(78, 0) Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@67e7625f.

You should change the build.Types part to this:

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

How to append version name in Android apk file name using Gradle?, Fix 1: Include archivesBaseName = “YourAppName-$versionName” in defaultConfig{}. defaultConfig { applicationId "your. 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.

If you don't specify versionName in defaultConfig block then defaultConfig.versionName will result in null

to get versionName from manifest you can write following code in build.gradle:

import com.android.builder.DefaultManifestParser

def manifestParser = new DefaultManifestParser()
println manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile)

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  Update (Gradle plugin 1.1.0) Android Gradle plugin version 1.1.0 introduced a bug which broke archivesBaseName, as friederbluemle noted. But fortunately this has since been fixed (in version 1.1.2). For some reason this stopped working with version 1.1.0-rc1 of the Android Gradle plugin.

android gradle change output apk file name. · GitHub, baseName.split("-");. def apkName = "ricebook-";. apkName += variants[0];. apkName += "-v" + android.defaultConfig.versionName;. if (!variant. Generate fully-flavoured apk filename with app_name, versionName, versionCode, git hash - build.gradle

android How to set versionName in APK filename using gradle , android How to set versionName in APK filename using gradle? android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { applicationId  Next, have Gradle generate a string resource for you automatically, which you can use in your about app’s XML layouts & Java code (as @string/app_version & getResources().getString(R.string.app

Android Studio 3.0, Android Studio 3.0 - Fix apk naming issue when upgrading to Gradle 3.0.0. Android Studio error “Cannot set the value of read-only property 'outputFile' for variant.outputs.each { output -> def finalVersionCode = 10000 + versionCode output. outputFile' to 'outputFileName'. android.applicationVariants.all { variant -> How to append version name in Android apk file name using Gradle?

Comments
  • I think this is the right approach instead of writing another task to rename files.
  • just change to archivesBaseName = "MyCompany-MyAppName-$versionName" if you have OCD and do not want AS warn you about the +
  • Great find, but doesn't work well with flavors with different version codes. They all end up with same version code.
  • Is there any way to add variant.buildType.name to the name? I know this isn't really default config related, but I'm trying to figure out how to remove the obsolete variantOutput.getAssemble() warning
  • Is it possible to remove from apk name last '-debug' / '-release'?
  • Do you know how to get it working if I have a versionName defined in AndroidManifest.xml instead of gradle config? It gives me myapp-release-null.apk now.
  • This answer doesn't work with 0.14+ versions of the gradle plugin. Any updates to work with those?
  • @withoutclass I asked this as it's own question and got it answered here: stackoverflow.com/questions/27068505/…
  • For people updating to Gradle 4: change each to all and output.outputFile to outputFileName. If somebody confims this works it can be edited into the answer :)
  • @PHPirate: almost works: Error:(34, 0) Cannot set the value of read-only property 'name'