多渠道打包

新上的项目,需要发布12个渠道,小米,魅族,华为等等。就涉及到多渠道打包问题。小结一下。

为啥要多渠道打包?

  • 添加渠道信息。我们要在安装包中添加不同的标识,应用在请求网络的时候携带渠道信息,方便后台做运营统计(这就是添加渠道信息的用处)。
  • 批量修改生成的apk文件名
  • 生成不同应用名称或图标

方法

umeng 渠道统计

android manifest

<meta-data
            android:name="UMENG_CHANNEL"
            android:value="umeng"/>

build.gradle

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.lemeng.reader.lemengreader"
        minSdkVersion 19
        targetSdkVersion 27
        multiDexEnabled true
        versionCode 1200
        versionName "1.2"
        flavorDimensions "versionCode"
        //动态配置
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
        ndk {
            //设置支持的SO库架构
            abiFilters 'armeabi', 'x86'//, 'armeabi-v7a', 'x86_64', 'arm64-v8a'
        }
    }


    signingConfigs {    //签名设置
        release {
            storeFile file("xxxx.keystore")
            storePassword "xxxxxx"
            keyAlias "xxxx"
            keyPassword "xxxxxx"
        }
        debug {
            storeFile file("xxxx.keystore")
            storePassword "xxxxxx"
            keyAlias "xxxx"
            keyPassword "xxxxxx"
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField("boolean", "LOG_DEBUG", "false")
            //签名设置
            signingConfig signingConfigs.release

            applicationVariants.all { variant ->
            	//定制化输出apk路径
                variant.getPackageApplication().outputDirectory = new File(project.rootDir.absolutePath+"/hmsq/release/")
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('release.apk')) {
                        def fileName = "${defaultConfig.applicationId}_${defaultConfig.versionName}_${defaultConfig.versionCode}_${variant.productFlavors[0].name}_release.apk"
                        output.outputFileName = new File(fileName)
                        println "输出文件位置: " + output.outputFile
                    }
                }
            }
        }

        debug {
            buildConfigField("boolean", "LOG_DEBUG", "true")
            minifyEnabled false

        }

    }

	//多个渠道设置
    productFlavors {
        // 友盟多渠道打包
        qq {}
        oppo {}
        vivo {}
        xiaomi {}
        meizu {}
        lenovo {}
        smartisan {}
        wandoujia{}
        _360 {}
        sogou {}
        huawei {}
    }

	//批量动态替换名字
    productFlavors.all { flavor ->
        flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

最后命令行执行 ./gradlew assembleRelease 即可