【问题标题】:Build release apk with customize name format in Android Studio在 Android Studio 中构建具有自定义名称格式的发布 apk
【发布时间】:2014-07-09 08:53:06
【问题描述】:

我希望.apk 使用以下名称格式(带有时间戳)构建。

如何设置?

格式:{app_name}{yyyymmddhis}.apk

现在,它被固定为名称{app_name}-{release}.apk

【问题讨论】:

    标签: android android-studio


    【解决方案1】:

    在build.gradle 文件中,您应该像这样更改/添加buildTypes:

    buildTypes {
          release {
            signingConfig signingConfigs.release
            applicationVariants.all { variant ->
                def file = variant.outputFile
                def date = new Date();
                def formattedDate = date.format('yyyyMMddHHmmss')
                variant.outputFile = new File(
                                        file.parent, 
                                        file.name.replace("-release", "-" + formattedDate)
                                        )
            }
        }       
    }
    


    ====== 使用 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.
    

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

    buildTypes {
            release {
                signingConfig signingConfigs.releaseConfig
                applicationVariants.all { variant ->
                    variant.outputs.each { output ->
                        def date = new Date();
                        def formattedDate = date.format('yyyyMMddHHmmss')
                        output.outputFile = new File(output.outputFile.parent, 
                                                output.outputFile.name.replace("-release", "-" + formattedDate)
                                                )
                    }
                }
            }
        }
    

    【讨论】:

    【解决方案2】:

    作为旁注,我真的很想知道人们从哪里获得 gradle build 对象的对象结构。

    为了构建它,我使用了一些试验和错误、一些谷歌搜索和this(已过时)

    android {
        ...
    
        buildTypes {
            debug {
                signingConfig signingConfigs.debug
            }
            release {
                signingConfig signingConfigs.release
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def flavor = "default";
                    if (variant.productFlavors.size() > 0)
                        flavor = variant.productFlavors.get(0);
    
                    def initials = "DefaultFlavor";
                    if (flavor.name == "flavor1")
                        initials = "F1";
                    else if (flavor.name == "flavor2")
                        initials = "F2";
    
                    def build = "Debug";
                    if (variant.buildType.name == "release")
                        build = "Release"
                    def finalName = variant.versionCode + "-" + initials + "-" + build + "-v" + flavor.versionName + "-MyAppName.apk";
                    output.outputFile = new File(output.outputFile.parent, finalName)
                }
            }
        }
        ...
    }
    

    好的,顺便说一句,如果您想确定您的 libs 项目的名称,这里有一个快速脚本,请确保将其放入您的每个库 gradle 构建文件中。

    android {
        ...
    
        defaultConfig {
            minSdkVersion 10
            targetSdkVersion 16
            versionCode 1
            versionName "1.0.4"
            project.version = versionName
        }
    
        libraryVariants.all { variant ->
            variant.outputs.each { output ->
                def outputFile = output.outputFile
                if (outputFile != null && outputFile.name.endsWith('.aar')) {
                    def fileName = "${archivesBaseName}-v${version}.aar"
                    output.outputFile = new File(outputFile.parent, fileName)
                }
            }
        }
    
        ...
    }
    

    【讨论】:

      【解决方案3】:

      你应该改变这样的东西。动态执行。

      project.archivesBaseName = {app_name}{yyyymmddhis};

      但我已经读到这将被弃用。

      按需创建属性(也称为动态属性)已被弃用,并计划在 Gradle 2.0 中移除。不推荐使用的动态属性:“根项目'myapp'”上的“archivesBaseName”,值:“AnotherName”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-06
        • 2019-01-24
        • 2019-03-18
        • 1970-01-01
        • 2018-06-10
        • 2017-03-18
        • 1970-01-01
        • 2016-04-05
        相关资源
        最近更新 更多