Android多渠道打包:gradle的配置

gradle方便的命令

在gradle環境下,Android打包可以很方便的使用命名行進行自動打包流程如:

.gradlew assemble

能自動打出所有包。
但是在實際使用中,我們希望不同的打包在程序內寫入不同的信息,比如修改全局的網絡請求等。

代碼註釋講解

android {
    signingConfigs {
        myconfig { // 定義一個簽名文件
            keyAlias 'xxxxxx'
            keyPassword 'xxxxxx'
            storeFile file('xxxxxx')
            storePassword 'xxxxxx'
            v2SigningEnabled falseo
        }
    }

    buildTypes {
        debug {
            // 其他代碼....
            signingConfig signingConfigs.myconfig // 使用指定簽名
        }
        release {
            // 其他代碼....
            signingConfig signingConfigs.myconfig
        }
    }

    productFlavors {
        // 定義兩個不同維度的打包類型渠道:哪些渠道,哪些服務器地址
        flavorDimensions 'channel', 'host'
        // 渠道
        hqyx {
            dimension 'channel'
            manifestPlaceholders.put('PACKAGE_CHANNEL_VALUE', 'hqyx') // 程序內部標記是哪個渠道
        }
        // 應用寶
        z_tengxun {
            dimension 'channel'
            manifestPlaceholders.put('PACKAGE_CHANNEL_VALUE', 'tengxun')
        }
        z_baidu {
            dimension 'channel'
            manifestPlaceholders.put('PACKAGE_CHANNEL_VALUE', 'baidu')
        }

        // 測試的服務器地址
        ttest {
            dimension 'host'
            buildConfigField('String', 'HOST', project.properties['environment.host.test'])
            // project.properties['environment.host.test'] 能讀取gradle.properties文件 內的配置信息
        }
        // 正式環境的服務器地址
        online {
            dimension 'host'
            buildConfigField('String', 'HOST', project.properties['environment.host.online'])
        }
    }
    // 控制打包時輸出的文件名
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            if (!variant.buildType.debuggable) {
                def outputFile = output.outputFile
                if (outputFile != null && outputFile.name.endsWith('.apk')) {
                    // 計算出新的文件名格式如:Stu_v1.3.0_02051437_develop_debug.apk
                    def fileName = 
                    "Stu_${defaultConfig.versionName}_${new Date().format("MMddHHmm")}_${variant.flavorName}.apk"
                    fileName = fileName.replace("_z_", "_")
                    output.outputFile = new File(outputFile.parent, fileName)
                }
            }
        }
    }
    // 配置多渠道時,去除沒用的Variants,比如線上環境一般不用出現在debug目錄下
    android.variantFilter { variant ->
        if (variant.buildType.name.contains('debug')) {
            variant.getFlavors().each() { flavor ->
                if (flavor.name.contains('z_')) {
                    variant.setIgnore(true)
                }
            }
        } else {
            variant.getFlavors().each() { flavor ->
                if (flavor.name.contains('develop')) {
                    variant.setIgnore(true)
                }
            }
        }
    }
}

查看gradle的配置效果

點擊AndroidStudio左下角的Build Variants即可查看所有的Variant
這裏寫圖片描述
在AndroidStudio下的Terminal輸入./gradlew assembleHqyxSimDebug即可輸入對應的安裝包

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章