如何使用gradle在APK文件名中设置versionName?

问题描述 投票:159回答:14

我正在尝试在gradle自动生成的APK文件名中设置特定的版本号。

现在gradle生成myapp-release.apk,但我希望它看起来像myapp-release-1.0.apk

我尝试重命名看起来很混乱的选项。有没有简单的方法可以做到这一点?

buildTypes {
    release {
       signingConfig signingConfigs.release
       applicationVariants.each { variant ->
       def file = variant.outputFile
       variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" +    defaultConfig.versionName + ".apk"))
    }
}

我没有运气尝试过上面的代码。有什么建议么?(使用gradle 1.6)

android build gradle apk android-gradle-plugin
14个回答
209
投票

我只需要在一个地方更改版本名称。代码也很简单。

下面的示例将创建名为MyCompany-MyAppName-1.4.8-debug.apkMyCompany-MyAppName-1.4.8-release.apk的APK文件,具体取决于所选的构建变体。] >

请注意,此解决方案可同时在APK和App Bundles (.aab files)上使用

另请参见:How to change the proguard mapping file name in gradle for Android project

最近的Gradle插件解决方案
android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "com.company.app"
        minSdkVersion 13
        targetSdkVersion 21
        versionCode 14       // increment with every release
        versionName '1.4.8'   // change with every release
        setProperty("archivesBaseName", "MyCompany-MyAppName-$versionName")
    }
}

以上解决方案已通过以下Android Gradle插件版本进行了测试:

  • 3.5.2(2019年11月)
  • 3.3.0(2019年1月)
  • 3.1.0(2018年3月)
  • 3.0.1(2017年11月)
  • 3.0.0(2017年10月)
  • 2.3.2(2017年5月)
  • 2.3.1(2017年4月)
  • 2.3.0(2017年2月)
  • 2.2.3(2016年12月)
  • 2.2.2
  • 2.2.0(2016年9月)
  • 2.1.3(2016年8月)
  • 2.1.2
  • 2.0.0(2016年4月)
  • 1.5.0(2015/11/12)
  • 1.4.0-beta6(2015/10/05)
  • 1.3.1(2015/08/11)
  • 随着新版本的发布,我将更新此帖子。

仅在版本1.1.3-1.3.0上测试过的解决方案

以下解决方案已通过以下Android Gradle插件版本进行了测试:

  • 1.3.0(2015/07/30)-不工作,bug scheduled to be fixed in 1.3.1
  • 1.2.3(2015/07/21)
  • 1.2.2(2015/04/28)
  • 1.2.1(2015/04/27)
  • 1.2.0(2015/04/26)
  • 1.2.0-beta1(2015/03/25)
  • 1.1.3(2015/03/06)
  • 应用程序gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId "com.company.app"
        minSdkVersion 13
        targetSdkVersion 21
        versionCode 14       // increment with every release
        versionName '1.4.8'   // change with every release
        archivesBaseName = "MyCompany-MyAppName-$versionName"
    }
}

3
投票

有很多答案完整或经过一些修改都是正确的。但是我还是要添加我的,因为我所有的人都遇到了问题,因为我正在使用脚本通过挂钩preBuild任务来动态生成VersionName和VersionCode。

如果您使用的是类似方法,则此代码将起作用:

project.android.applicationVariants.all { variant ->
    variant.preBuild.doLast {
    variant.outputs.each { output ->
        output.outputFile = new File(
                output.outputFile.parent,
                output.outputFile.name.replace(".apk", "-${variant.versionName}@${variant.versionCode}.apk"))
        }
    }
}

2
投票
    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            output.outputFileName = output.outputFileName.replace(".apk", "-${variant.versionName}.apk")
        }
    }

1
投票

就我而言,我通过这种方式解决了此错误


0
投票

对于最新的gradle版本,您可以使用以下代码段:

首先设置您的应用程序清单位置

 sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
        {
    }

0
投票

自Android Studio 1.1.0起,我发现此组合在build.gradle文件的android主体中有效。如果您不知道如何导入清单xml文件数据。我希望它能得到Android Studio的更多支持,但请尝试使用这些值,直到获得所需的apk名称输出为止:

defaultConfig {
        applicationId "com.package.name"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 6
        versionName "2"
    }
    signingConfigs {
        release {
            keyAlias = "your key name"
        }
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            signingConfig signingConfigs.release
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace("app-release.apk", "appName_" + versionName + ".apk"))
                }
            }
        }
    }

171
投票

这解决了我的问题:使用applicationVariants.all而不是applicationVariants.each


41
投票

(已编辑,可与Android Studio 3.0和Gradle 4一起使用


18
投票

总结起来,对于那些不知道如何在build.gradle中导入包的人(像我一样,请使用以下buildTypes


17
投票

如果未在defaultConfig块中指定versionName,则defaultConfig.versionName将导致null


8
投票

就我而言,我只是想找到一种方法来自动生成apkrelease变体的不同debug名称。通过将此代码段作为android的子代码,我设法轻松地做到了这一点:

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        def appName = "My_nice_name_"
        def buildType = variant.variantData.variantConfiguration.buildType.name
        def newName
        if (buildType == 'debug'){
            newName = "${appName}${defaultConfig.versionName}_dbg.apk"
        } else {
            newName = "${appName}${defaultConfig.versionName}_prd.apk"
        }
        output.outputFile = new File(output.outputFile.parent, newName)
    }
}

6
投票

四年级


5
投票

另一种替代方法是使用以下方法:


5
投票

根据@Jon answer重命名apk的正确方法>

defaultConfig {
        applicationId "com.irisvision.patientapp"
        minSdkVersion 24
        targetSdkVersion 22
        versionCode 2  // increment with every release
        versionName "0.2" // change with every release
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        //add this line
        archivesBaseName = "AppName-${versionName}-${new Date().format('yyMMdd')}"
    }   
© www.soinside.com 2019 - 2024. All rights reserved.