【问题标题】:How to generate output APK file with Customised name in Android Studio?如何在 Android Studio 中生成具有自定义名称的输出 APK 文件?
【发布时间】:2019-01-24 02:23:30
【问题描述】:

我想使用自定义名称生成输出 apk 文件。例如。默认情况下,android studio 会生成“app-debug.apk”文件。但我希望它是 - “MyAppName_myCurrentProdFlavour_vMyVersionName.apk” 如何使用模块的 build.gradle 文件来做到这一点

【问题讨论】:

    标签: android android-studio android-gradle-plugin build.gradle


    【解决方案1】:

    最简单的方法

    app_debug.apk

    之前

    com.pcvark.jobtest-v1(1.0)-debug.apk 之后

    android {
        ..
        defaultConfig {
            ...
            setProperty("archivesBaseName", applicationId + "-v" + versionCode + "(" + versionName + ")")
        }
    

    提示您还可以为不同的buildType 和变体(如果有)设置versionNameSuffix。这将为debug 和release 生成不同的命名apk。

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

    进一步阅读

    https://stackoverflow.com/a/20660274/6891563

    https://docs.gradle.org/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html

    【讨论】:

    • 如何在defaultConfig中获取applicationName?
    • 谢谢你。显然语法从 project.ext.set() 更新为 setProperty() 在某些时候。
    【解决方案2】:

    您可以使用此自定义 apk 文件名:

    转到应用程序 Gradle

      setProperty("archivesBaseName", "yourapp-packagename-$versionName")
    
    android {
        compileSdkVersion 22
        buildToolsVersion "22.0.1"
        defaultConfig {
            applicationId "com.test.app"
            minSdkVersion 13
            targetSdkVersion 21
            versionCode 14       // increment with every release
            versionName '1.4.8'   // change with every release
            setProperty("archivesBaseName", "yourapp-packagename-$versionName")
        }
    

    }

    【讨论】:

      【解决方案3】:

      您可以将以下机制分别用于debug 和release 构建,这将允许您为每个build 提供不同的名称。

      buildTypes {
              debug {
                  minifyEnabled false
                  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                  applicationVariants.all { variant ->
                      variant.outputs.each { output ->
                          project.ext { appName = 'MY-CUSTOM-APP' }
                          def newName = output.outputFile.name
                          newName = newName.replace("app-", "$project.ext.appName-")
                          output.outputFile = new File(output.outputFile.parent, newName)
                      }
                  }
                  signingConfig signingConfigs.MyApp
              }
              release {
                  debuggable false
                  jniDebuggable false
                  applicationVariants.all { variant ->
                      variant.outputs.each { output ->
                          project.ext { appName = 'MY-CUSTOM-APP' }
                          def newName = output.outputFile.name
                          newName = newName.replace("app-", "$project.ext.appName-")
                          output.outputFile = new File(output.outputFile.parent, newName)
                      }
                  }
                  signingConfig signingConfigs.MyApp
                  minifyEnabled true
                  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
              }
          }
      

      乐于助人!!!

      【讨论】:

        【解决方案4】:

        1.更改应用包名称(.aab 文件):

        在 app 模块的 build.gradle 中:

        def dateFormat = new Date().format('ddMMMyy_HHmm')
        defaultConfig {
                applicationId "com.xyz"
                minSdk 21
                targetSdk 31
                versionCode 3
                versionName "1.0.2"
        
                setProperty("archivesBaseName", "yourappname" + "_v" + versionName + "(" + versionCode + ")_"+dateFormat)
        
            }
        

        例如:如果您的应用名称是 XYZ,那么会生成带有名称的包名称:XYZ_v1.0.2(3)_28Jan22_1152-prod-release

        在哪里, yourappname 是 XYZ , versionName 是 1.0.2 , versionCode 为 3 , dateFormat 是 28Jan22_1152 , 并且应用 flavorType 和 buildType 是 prod-release

        2。用于更改 apk 文件名(.apk 文件)

        在 app 模块的 build.gradle 中:

        android {
        
        applicationVariants.all { variant ->
                    variant.outputs.each { output ->
                        def name = "XYZ"
                        def SEP = "_"
                        def flavor = variant.productFlavors[0].name
                        def buildType = variant.buildType.name
                        def versionName = defaultConfig.versionName
                        def versionCode = defaultConfig.versionCode
                        def newApkName = name + SEP + "v" + versionName + SEP + flavor + SEP + buildType + SEP + "vc" + versionCode + SEP + dateFormat + ".apk"
                        output.outputFileName = newApkName
                    }
                }    
        }
        

        例如,如果您的应用名称是 XYZ,那么这将生成具有以下自定义名称的 APK 文件: XYZ_v1.0.2_prod_release_vc3_28Jan22_1152


        注意:您可以根据需要更改 dateFormat,如果您没有 productFlavours,则可以更改 apk 名称,然后从上面给出的代码中删除所有相关引用。

        【讨论】:

          【解决方案5】:

          APK 名称及版本、日期和时间。

          defaultConfig {
              def date = new Date()
              def formattedDate = date.format('ddMMYYYY')
              def formattedVersion = versionName
              formattedVersion = formattedVersion.replaceAll('\\.','')
              setProperty("archivesBaseName", "ApplicationName_"+formattedVersion+"_"+formattedDate)
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-12-03
            • 2021-12-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-01-08
            相关资源
            最近更新 更多