android 多渠道打包不同的包名、应用名、应用图标

android 开发者都会碰到多渠道打包的需求,多渠道打包的流程大致如下:

在build.gradle文件中的与defaultConfig同级添加:productFlavors{渠道1 渠道2};

这是最简单的添加多渠道打包过程,需求稍变一下,我需要同一个手机,安装多个app。这里,这样简单的配置就不够了,需要做定制化处理了
eg:productFlavors{
xiaomi {
manifestPlaceholders = [str: "渠道名称", package_name: "包名"]
applicationId "包名"
}
huawei {
manifestPlaceholders = [str: "渠道名称", package_name: "包名"]
applicationId "包名"
}....
再加上此配置校验build
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}

如果你需要配置app名字、版本、等信息,需要增加如下配置:
eg:`android.applicationVariants.all { variant ->

    variant.outputs[0].processManifest.doLast {
        //${buildDir}是指build文件夹
        //${variant.dirName}是flavor/buildtype,例如GooglePlay/release,运行时会自动生成
        //下面的路径是类似这样:build/manifests/GooglePlay/release/AndroidManifest.xml
        def manifestFile = "${buildDir}/intermediates/manifests/full/${variant.dirName}/AndroidManifest.xml"
        //将字符串channel替换成flavor的名字
        def flavorsName = "${variant.productFlavors[0].name}"
        def name = flavorsName.split('_')
        def channelUpdatedContent = new File(manifestFile).getText('UTF-8')
                .replaceAll("CHANNEL_VALUE", "${name[0]}")
                .replaceAll("REFERENCE_VALUE", "${name[1]}")
        new File(manifestFile).write(channelUpdatedContent, 'UTF-8')
    }

    variant.outputs.all {
        outputFileName = "app_v${defaultConfig.versionName}_${variant.name}_${releaseTime()}.apk"
    }
}`

拓展:如遇到不同市场,不同应用图标的情况,首先,为你默哀4s
其实也不是特别麻烦,如果市场多可能需要的时间会长一些,但配置一次之后,后面更改就方便很多了

eg:productFlavors.each {
flavor ->
//需要单独处理图标、名字、友盟渠道的应用,还可增加其他属性
if (flavor.name == "这里填写productFlavors里面的渠道名") {
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name,
app_icon : "@mipmap/ic_launcher_vivo",
app_name : "@string/app_name"
]
}....
}

重点内容
不同市场图标问题解决,最后,需要在配置文件AndroidManifest配置

<application
        android:name="com.xxx.Application"
        android:icon="${app_icon}"
        android:label="${app_name}"
        android:theme="@style/AppTheme"
        tools:replace="name,icon,label">
        </application>

一定要在tools:replace里面添加你要动态添加的属性名称,否则会编译出问题的

完毕,如有问题或者建议,请留言

猜你喜欢

转载自blog.csdn.net/u013346208/article/details/80761204