How to change apk name by using gradle like this format?

后端 未结 3 621
死守一世寂寞
死守一世寂寞 2021-01-30 05:07

I\'d like to change \"app-release.apk\" file name to like following when I build an app by using gradle.

[format]
(appname of package name)_V(version code)_(yyMMdd)_(         


        
相关标签:
3条回答
  • 2021-01-30 05:48

    2019 / 2020 - How to change APK name For Gradle 3.3, 3.4, 3.5, 4.0 and above

    android {
       ......
       applicationVariants.all { variant ->
           variant.outputs.all {
               def flavor = variant.name
               def versionName = variant.versionName
               outputFileName = "prefix_${flavor}_${versionName}.apk"
           }
       }
    }
    

    The result would be like this,

    prefix_release_1.0.1.apk

    0 讨论(0)
  • 2021-01-30 05:52

    This may be the shortest way:

    defaultConfig {
        ...
        applicationId "com.blahblah.example"
        versionCode 1
        versionName "1.0"
        setProperty("archivesBaseName", applicationId + "-v" + versionCode + "(" + versionName + ")")
    }
    

    buildType: like so

    buildTypes {
        debug {
            ...
            versionNameSuffix "-T"
        }
        release {
            ...
            versionNameSuffix "-R"
        }
    }
    

    Keep in mind, Android Studio adds versionNameSuffix by build type name by default, so you may not need this.

    Upd. In new versions of Android Studio you can it write little shorter(thanks for szx comment):

    defaultConfig {
        ...
        archivesBaseName = "$applicationId-v$versionCode($versionName)"
    }
    
    0 讨论(0)
  • 2021-01-30 05:58

    Update: Please check Anrimian's answer below which is much simpler and shorter.

    Try this:

    gradle.properties

    applicationName = MyApp
    

    build.gradle

    android {
      ...
      defaultConfig {
         versionCode 111
         ...
      }
      buildTypes {
         release {
             ...
             applicationVariants.all { variant ->
                 renameAPK(variant, defaultConfig, 'R')
             }
         }
         debug {
             ...
             applicationVariants.all { variant ->
                 renameAPK(variant, defaultConfig, 'T')
             }
         }
      }
    }
    def renameAPK(variant, defaultConfig, buildType) {
     variant.outputs.each { output ->
         def formattedDate = new Date().format('yyMMdd')
    
         def file = output.packageApplication.outputFile
         def fileName = applicationName + "_V" + defaultConfig.versionCode + "_" + formattedDate + "_" + buildType + ".apk"
         output.packageApplication.outputFile = new File(file.parent, fileName)
     }
    }
    

    Reference: https://stackoverflow.com/a/30332234/206292 https://stackoverflow.com/a/27104634/206292

    0 讨论(0)
提交回复
热议问题