Build release apk with customize name format in Android Studio

前端 未结 3 1940
面向向阳花
面向向阳花 2021-02-07 16:11

I want the .apk to be built with the following name format (with timestamp).

How can I set it?

format : {app_name}{yyyymmddhis}.apk

相关标签:
3条回答
  • 2021-02-07 16:16

    in the build.gradle file, you should change/add buildTypes like this:

    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)
                                        )
            }
        }       
    }
    


    ====== EDIT with Android Studio 1.0 ======

    If you are using Android Studio 1.0, you will get an error like this:

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

    You should change the build.Types part to this:

    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)
                                                )
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2021-02-07 16:27

    As a side note, I would really like to know where people get the objects structures of gradle build objects.

    To construct this I've used some trial and errors, a bit of Googling, and this (which is out of date)

    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)
                }
            }
        }
        ...
    }
    

    Ok, So as a side note, if you would like to determine the names of your libs project here is a quick script, make sure you put it in each of your library gradle build files.

    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)
                }
            }
        }
    
        ...
    }
    
    0 讨论(0)
  • 2021-02-07 16:38

    You should change something like this. Doing dynamically.

    project.archivesBaseName = {app_name}{yyyymmddhis};

    But I have read that this is going to be deprecated.

    Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Deprecated dynamic property: "archivesBaseName" on "root project 'myapp'", value: "AnotherName".

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