android studio 3.0 gradle 打包腳本配置

修改輸出的名字 保存輸出的文件路徑android

def fileArray = []
//遍歷輸出文件    
android.applicationVariants.all { variant ->
    variant.outputs.all { output ->
        def outputFile = output.outputFile
        if (outputFile != null && outputFile.name.endsWith('release.apk')) {
            def fileName = "xxx_${defaultConfig.versionName}_${defaultConfig.versionCode}" +
                    "_${variant.productFlavors[0].name}.apk"
            outputFileName = fileName
            //往數組添加輸出的文件路徑
            fileArray.add(outputFile.parentFile.absolutePath + File.separator + fileName)
        }
    }複製代碼

一般多渠道打包後須要輸出文件 並保存最新的apk在一個文件夾中
build命令編譯生成debug和release版本的包
assembleRelease命令只編譯生成release版本的包數組

build {
//build命令加入這段代碼
    doLast() {
        //遍歷文件數組並進行操做
        forEachFile(fileArray)
    }
}
//assembleRelease命令加入這段代碼
afterEvaluate {
    assembleRelease.doLast {
        //遍歷文件數組並進行操做
        forEachFile(fileArray)
    }
}
def forEachFile(fileArray) {
    fileArray.forEach { file ->
    //遍歷進行文件操做
    rename_andd_moveout_apk(file)
}複製代碼

}
能夠使用copy rename進行文件操做bash

def rename_andd_moveout_apk(orignalFile) {
    def intoFile = rootDir.parentFile.getAbsolutePath()+File.separator+"apk"
    copy {
        from orignalFile
        into intoCodeFile
        rename("${android.defaultConfig.versionName}_ ${android.defaultConfig.versionCode}_", "")
       }
    }        複製代碼

能夠經過gradle腳本動態配置Mainfest裏面一些第三方配置,這樣能夠實現DEV和PRD多環境配置切換
使用:android.defaultConfig.manifestPlaceholders=["key":"value"]
在Mainfest使用${key}引用你要使用的第三方配置數據,開發過程當中一般應用於個推、環信等多環境的配置app

有錯誤的地方,望你們指正!
t.cn/Rlvl2Zugradle

相關文章
相關標籤/搜索