Build release apk with customize name format in Android Studio

前端 未结 3 1941
面向向阳花
面向向阳花 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: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)
                }
            }
        }
    
        ...
    }
    

提交回复
热议问题