android——利用gradle实现多渠道打包并自定义包名(umeng多渠道)

时间:2024-03-03 07:18:16

1.首先在mainfest.xml文件中,给umeng_channel设置一个变量“${UMENG_CHANNEL_VALUE}

2.在gradle中,配置flavors。

 productFlavors {
        yingyongbao {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "bjtq_txyyb"]
      
        }
        s360 {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "bjtq_360"]
       
        }
        baidu {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "bjtq_baidu"]
     
        }
        xiaomi {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "bjtq_xiaomi"]
           
        }
        huawei {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "bjtq_huawei"]
          
        }
        meizu {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "bjtq_meizu"]
          
        }
        lenovo {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "bjtq_len"]
            applicationId \'com.songheng.beijingtianqikuaibao\'
        }
        oppo {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "bjtq_oppo"]

        }
        vivo {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "bjtq_vivo"]

        }
        ali {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "bjtq_ali"]

        }
        yingyonghui {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "bjtq_yyh"]

        }
        sougou {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "bjtq_sougou"]

        }
        anzhi {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "bjtq_anzhi"]
        }
    }

3.可以直接签名打包了。

这么打包,apk所在路径:项目路径\app\app-anzhi-release.apk 

4.自定义包名方式

  applicationVariants.all { variant ->
        variant.outputs.each { output ->
            if (\'release\'.equals(variant.buildType.name)) {
                def outputFile = output.outputFile
                if (outputFile != null && outputFile.name.endsWith(\'.apk\')) {

                    def buildLable = ""
                    def buildName = ""

                    // 是否是连接测试服务器版本
                    if (rootProject.hasProperty(\'TestServer\')) {
                        buildLable = "${buildLable}_Test"
                    } else {
                        buildLable = "${buildLable}_Product"
                    }
            //buildname 是channel名称
                    variant.productFlavors.each { product ->
                        buildName = product.name
                    }

                    // 是否是testin版本
                    if (rootProject.hasProperty(\'TestIn\')) {
                        buildLable = "${buildLable}_TestIn"
                    }
                    def productName = "bjtqyb"
                    def fileName = "${productName}_v${defaultConfig.versionName}_${releaseTime()}${buildLable}_${buildName}.apk"
                    output.outputFile = new File("${rootDir}/outapks/", fileName)
                }
            }
        }
    }