简体   繁体   中英

How do I append the build.gradle “versionName” to the release build APK file

In my app level build.gradle file I have the version name, such as

versionName "1.03"

When I build a signed release .apk, in Android Studio, the output file is named app-release.apk and is stored in the release directory. How can I update the Gradle build so that the output file is named my_application_1.03.apk

Thanks

If you want to change your apk file name at release build you can do like as follows.

For Example: ( app/build.gradle )

android {
applicationVariants.all { variant ->
    if (variant.buildType.name.equals("release")) {
        variant.outputs.each { output ->
            if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
                def apk_name = "my_application"
                def versionName = defaultConfig.versionName
                //you can also add version code and date if you like
                //def applicationId = defaultConfig.applicationId
                //def versionCode = defaultConfig.versionCode
                //def date = new java.text.SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())
                def newName = "${apk_name}_${versionName}.apk"
                output.outputFile = new File(output.outputFile.parent, newName)
            }
        }
     }
  }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM