Android中利用productFlavors配置多渠道

build.gradle(app)设置

android {
    flavorDimensions "default"
    productFlavors{
        //不同渠道
        360{
            dimension "default"
            //替换AndroidManifest中的值
            manifestPlaceholders = [channel_value:"360"]
        }
        qq{
            dimension "default"
            //替换AndroidManifest中的值
            manifestPlaceholders = [channel_value:"qq"]
        }
    }
}

AndroidMainfest设置

    <application
        ...
        "CHANNEL_VALUE" android:value="${channel_value}"/>
        ...
    

代码中获取渠道值

    /**
     * 从Manifest中获取meta-data值
     * @param context
     * @param key
     * @return
     */
    public static String getMetaData(Context context,String key) {
        String value = null;
        try {
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
                    context.getPackageName(), PackageManager.GET_META_DATA);
            value = appInfo.metaData.getString(key);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (value==null) value="";
        return value;
    }

转载于:https://www.cnblogs.com/Potato-Eater/p/9790874.html

你可能感兴趣的:(Android中利用productFlavors配置多渠道)