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

cod*_*R0y 159 android build gradle apk android-gradle-plugin

我正在尝试在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"))
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Jon*_*Jon 209

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

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

请注意,此解决方案适用于APK和应用程序包(.aab文件).

另请参阅:如何在gradle for Android项目中更改proguard映射文件名

最近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")
    }
}
Run Code Online (Sandbox Code Playgroud)

上述解决方案已使用以下Android Gradle插件版本进行测试:

  • 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) - 不工作,错误计划在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)

app 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"
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为这是正确的方法,而不是编写另一个任务来重命名文件. (11认同)
  • 如果您有强迫症,只需更改为archivesBaseName ="MyCompany-MyAppName- $ versionName",并且不希望AS警告您关于+ (5认同)
  • 很好的发现,但不适用于具有不同版本代码的风味.它们都以相同的版本代码结尾. (4认同)
  • 有什么办法可以将 `variant.buildType.name` 添加到名称中吗?我知道这并不是真正的默认配置相关,但我试图弄清楚如何删除过时的 `variantOutput.getAssemble()` 警告 (3认同)
  • 是否可以从 apk 名称中删除最后一个“-debug”/“-release”? (3认同)

cod*_*R0y 171

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

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

更新:

因此,似乎这不适用于0.14+版本的android studio gradle插件.

这样就可以了(参考这个问题 ):

android {
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File(
                    output.outputFile.parent,
                    output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @PHPirate:几乎可以工作:`错误:(34,0)无法设置只读属性'name'的值 (6认同)
  • 如果我在`AndroidManifest.xml`中定义`versionName`而不是gradle config,你知道如何使它工作吗?它现在给了我'myapp-release-null.apk`. (3认同)
  • 对于更新为Gradle 4的人:将`each`更改为`all`和`output.outputFile`更改为`outputFileName`.如果有人认可这个作品,它可以编辑成答案:) (2认同)

Fer*_*Fer 41

(已编辑,可与Android Studio 3.0和Gradle 4配合使用)

我正在寻找一个更复杂的apk文件名重命名选项,我写了这个,希望它对任何人都有帮助.它使用以下数据重命名apk:

  • 味道
  • 构建类型
  • 日期

它花了我一些关于gradle类的研究和一些来自其他答案的复制/粘贴.我正在使用gradle 3.1.3.

在build.gradle中:

android {

    ...

    buildTypes {
        release {
            minifyEnabled true
            ...
        }
        debug {
            minifyEnabled false
        }
    }

    productFlavors {
        prod {
            applicationId "com.feraguiba.myproject"
            versionCode 3
            versionName "1.2.0"
        }
        dev {
            applicationId "com.feraguiba.myproject.dev"
            versionCode 15
            versionName "1.3.6"
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def project = "myProject"
            def SEP = "_"
            def flavor = variant.productFlavors[0].name
            def buildType = variant.variantData.variantConfiguration.buildType.name
            def version = variant.versionName
            def date = new Date();
            def formattedDate = date.format('ddMMyy_HHmm')

            def newApkName = project + SEP + flavor + SEP + buildType + SEP + version + SEP + formattedDate + ".apk"

            outputFileName = new File(newApkName)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你今天(10-10-16)在10:47编译,你会得到以下文件名,具体取决于你选择的风味和构建类型:

  • dev debug:myProject_ dev_debug_1.3.6 _131016_1047.apk
  • 开发版:myProject_ dev_release_1.3.6 _131016_1047.apk
  • prod debug:myProject_ prod_debug_1.2.0 _131016_1047.apk
  • prod release:myProject_ prod_release_1.2.0 _131016_1047.apk

注意:未对齐版本的apk名称仍然是默认名称.

  • 解决错误**无法设置只读属性'outputFile'的值** - 如前面的注释所述,必须*"将`each`更改为`all`和`output.outputFile`为`outputFileName` "* - 这篇文章提供了一些细节:/sf/answers/3098576211/ (3认同)

Wes*_*ley 18

总而言之,对于那些不知道如何导入包的人build.gradle(比如我),请使用以下内容buildTypes,

buildTypes {
      release {
        signingConfig signingConfigs.release
        applicationVariants.all { variant ->
            def file = variant.outputFile
            def manifestParser = new com.android.builder.core.DefaultManifestParser()
            variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile) + ".apk")) 
        }
    }       
}
Run Code Online (Sandbox Code Playgroud)

=====编辑=====

如果您在文件中设置versionCodeversionName,build.gradle就像这样:

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 19
    versionCode 1
    versionName "1.0.0"
}
Run Code Online (Sandbox Code Playgroud)

你应该这样设置:

buildTypes {   
        release {
            signingConfig signingConfigs.releaseConfig
            applicationVariants.all { variant ->
                def file = variant.outputFile
                variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
            }
        }
}
Run Code Online (Sandbox Code Playgroud)


======使用Android Studio 1.0编辑======

如果您使用的是Android Studio 1.0,则会出现如下错误:

Error:(78, 0) Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@67e7625f.
Run Code Online (Sandbox Code Playgroud)

您应该将build.Types部件更改为:

buildTypes {
        release {
            signingConfig signingConfigs.releaseConfig
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


Ark*_*ior 17

如果未在defaultConfig块中指定versionName,defaultConfig.versionName则会生成null

要从manifest中获取versionName,您可以在build.gradle中编写以下代码:

import com.android.builder.DefaultManifestParser

def manifestParser = new DefaultManifestParser()
println manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile)
Run Code Online (Sandbox Code Playgroud)

  • 我相信对于更高版本的gradle,它现在是com.android.builder.core.DefaultManifestParser (7认同)

ysh*_*hak 8

就我而言,我只是想找到一种方法来自动生成不同的apk名称releasedebug变体.我设法通过将此代码段作为以下子代轻松完成此操作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)
    }
}
Run Code Online (Sandbox Code Playgroud)

对于新的Android gradle插件3.0.0,您可以执行以下操作:

 applicationVariants.all { variant ->
    variant.outputs.all {
        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"
        }
        outputFileName = newName
    }
}
Run Code Online (Sandbox Code Playgroud)

这会产生类似于: My_nice_name_3.2.31_dbg.apk


PHP*_*ate 6

Gradle 4

语法已经改变了一点在摇篮4(Android Studio中3+)(来自output.outputFileoutputFileName,从构思这个答案现在是:

android {
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def newName = outputFileName
            newName.replace(".apk", "-${variant.versionName}.apk")
            outputFileName = new File(newName)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Mar*_* RS 5

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

String APK_NAME = "appname"
int VERSION_CODE = 1
String VERSION_NAME = "1.0.0"

project.archivesBaseName = APK_NAME + "-" + VERSION_NAME;

    android {
      compileSdkVersion 21
      buildToolsVersion "21.1.1"

      defaultConfig {
        applicationId "com.myapp"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode VERSION_CODE
        versionName VERSION_NAME
      }

       .... // Rest of your config
}
Run Code Online (Sandbox Code Playgroud)

这将为您的所有apk输出设置"appname-1.0.0".


Qam*_*mar 5

按照@Jon答案重命名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')}"
    }   
Run Code Online (Sandbox Code Playgroud)

或者通过另一种方法可以达到相同的结果

android {
    ...

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def formattedDate = new Date().format('yyMMdd')
            outputFileName = "${outputFileName.replace(".apk","")}-v${defaultConfig.versionCode}-${formattedDate}.apk"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

77200 次

最近记录:

5 年,11 月 前