Build release apk with customize name format in Android Studio

android, android-studio

Solution

in the `build.gradle` file, you should change/add `buildTypes` like this:

buildTypes {
      release {
        signingConfig signingConfigs.release
        applicationVariants.all { variant ->
            def file = variant.outputFile
            def date = new Date();
            def formattedDate = date.format('yyyyMMddHHmmss')
            variant.outputFile = new File(
                                    file.parent, 
                                    file.name.replace("-release", "-" + formattedDate)
                                    )
        }
    }       
}

====== 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 ->
                    def date = new Date();
                    def formattedDate = date.format('yyyyMMddHHmmss')
                    output.outputFile = new File(output.outputFile.parent, 
                                            output.outputFile.name.replace("-release", "-" + formattedDate)
                                            )
                }
            }
        }
    }

Problem

I want the `.apk` to be built with the following name format (with timestamp). How can I set it? format : `{app_name}{yyyymmddhis}.apk` Now, it is fixed with the name `{app_name}-{release}.apk`

Original source