简体   繁体   中英

Build release apk with customize name format in Android Studio

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

How can I set it?

format : {app_name}{yyyymmddhis}.apk

Now, it is fixed with the name {app_name}-{release}.apk

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

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

    ...
}

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 (aka 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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM