自定义输出apk配置

2017-10-18  本文已影响52人  SScience

当我们在输出apk时(包括Build APK和Generate Signed APK),AS默认的apk文件名是app-debug.apk或app-release.apk,为了自定义输出apk的配置,可以修改如下:
1,android.tools.build:gradle的版本小于3.0时

android {
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                 // 输出apk名称为eshifu_v1.0_release_releaseTime.apk
                 def fileName = "eshifu_v${defaultConfig.versionName}_${variant.buildType.getName()}_${releaseTime()}.apk"
                 println "The output apk name: " + fileName
                 output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    } 
}

static def releaseTime() {
    return new Date().format("yyyyMMddHHmmss")
}

2,android.tools.build:gradle的版本为3.0时

android {
    android.applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                // 输出apk名称为eshifu_v1.0_release_releaseTime.apk
                def fileName = "eshifu_v${defaultConfig.versionName}_${variant.buildType.getName()}_${releaseTime()}.apk"
                println "The output apk name: " + fileName
                outputFileName = fileName
            }
        }
    }
}

static def releaseTime() {
    return new Date().format("yyyyMMddHHmmss")
}
上一篇下一篇

猜你喜欢

热点阅读