android Gradle打包修改生成的apk文件名称

直接上代码:


import java.text.SimpleDateFormat

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.dsy.myapplication"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def file = output.outputFile
            output.outputFile = new File(file.parent,
                    "myapp-" + defaultConfig.versionName + "-" + buildTime() + ".apk")
            //生成的文件名:myapp+版本名+生成日期+.apk
        }
    }
}

def buildTime() {
    def df = new SimpleDateFormat("yyyyMMdd")
    df.setTimeZone(TimeZone.getDefault())
    return df.format(new Date())
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:23.1.1'
}


你可能感兴趣的:(android)