多渠道配置时获取productFlavors中配置的字段的值

在项目中设计多渠道打包,需要把不同渠道的ChannelID字段传给后台,用于不同平台的数据统计,就此记录下配置方法以及如何在代码中获取配置的字段。

buidl.gradle配置如下:
	def releaseTime() {
 	   return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
	}
  android.applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                def fileName
                def packageLastName = applicationId.substring(applicationId.lastIndexOf(".") + 1, applicationId.length())
                println(">>>>>>>>>>>>>packageLastName:" + packageLastName)
                if (variant.buildType.name == "release") {

                    fileName = packageLastName.toUpperCase() + "_${variant.productFlavors[0].name.toUpperCase()}_V${defaultConfig.versionName}_RELEASE_" + releaseTime() + ".apk"
                } else {
                    fileName = packageLastName + "_${variant.productFlavors[0].name}" + "_v${defaultConfig.versionName}_${variant.buildType.name}_" +releaseTime() + ".apk"
                }
                if (variant.buildType.name == "release")
                    variant.getPackageApplication().outputDirectory = new File(project.rootDir.absolutePath + "/apk/" + variant.buildType.name)
                outputFileName = fileName
            }
        }
    }

    //定义不同产品的不同维度,没有该需求,定义为空
    flavorDimensions("")
    //渠道Flavors,配置不同风格的app
    productFlavors {

        only_test {

            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.releaseChannel]
        }

        meizu {

            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.meizuChannel]
        }


        yyb{

            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.yybChannel]
        }
        samsung {

            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.meizuChannel]
        }
        jinli{
            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.jinliChannel]
        }
        baidu{

            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.baiduChannel]
        }
        ali{

            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.aliChannel]
        }
        anzhi {

            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.anzhiChannel]
        }
        official {

            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.officialChannel]
        }
        hauwei {

            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.hauweiChannel]
        }
        oppo {

            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.oppoChannel]
        }
        vivo {

            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.vivoChannel]
        }
        xiaomi {

            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.xiaomiChannel]
        }
        _360{

            manifestPlaceholders = [ CHANNEL_ID: rootProject.ext._360Channel]
        }
    }

manifestPlaceholders字段记得在manifest.xml文件中配置

    <meta-data
        android:name="CHANNEL_ID"
        android:value="${CHANNEL_ID}" />

配置完后,我们就可以去获取CHANNEL_ID这个字段的值了,下面是获取的工具类:

public class AppChannelUtil {
    public static String getChannelId() {
        return getMetaDataStr("CHANNEL_ID");
    }

    public static String getMetaDataStr(String key) {
        String resultData = "";
        if (!TextUtils.isEmpty(key)) {
            Bundle appInfoBundle = getAppInfoBundle();
            if (appInfoBundle != null)
                resultData = appInfoBundle.getString(key);
        }
        LogUtil.i("AppChannelUtil","渠道号--------"+resultData);
        return resultData;
    }

    public static int getMetaDataInt(String key) {
        int resultData = 0;
        if (!TextUtils.isEmpty(key)) {
            Bundle appInfoBundle = getAppInfoBundle();
            if (appInfoBundle != null)
                resultData = appInfoBundle.getInt(key);
        }
        return resultData;
    }

    private static Bundle getAppInfoBundle() {
//注意:这里ApplicationInfo不能直接 CommonApplication.mApplication.getApplicationInfo()这样获取,否则会获取不到meta_data的
        ApplicationInfo applicationInfo = getAppInfo();
        if (applicationInfo != null) {
            return applicationInfo.metaData;
        }
        return null;
    }

    private static ApplicationInfo getAppInfo() {
        PackageManager packageManager = CommonApplication.mApplication.getPackageManager();
        ApplicationInfo applicationInfo = null;
        if (packageManager != null) {
            try {
                applicationInfo = packageManager.getApplicationInfo(CommonApplication.mApplication.getPackageName(), PackageManager.GET_META_DATA);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return applicationInfo;
    }
}

获取其他数据类型的值也是如此!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值