Build release apk with customize name format in Android Studio

前端 未结 3 1945
面向向阳花
面向向阳花 2021-02-07 16:11

I want the .apk to be built with the following name format (with timestamp).

How can I set it?

format : {app_name}{yyyymmddhis}.apk

3条回答
  •  猫巷女王i
    2021-02-07 16:16

    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)
                                                )
                    }
                }
            }
        }
    

提交回复
热议问题