美文网首页Android知识
AndroidStudio多渠道打包build.gradle设置

AndroidStudio多渠道打包build.gradle设置

作者: OoYoO | 来源:发表于2017-11-30 18:15 被阅读17次

    对应Manifest文件中的Umeng渠道

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

    注意UMENG_CHANNEL_VALUE前后的是大括号{}

    AndroidStudio3.0上进行多渠道打包必须指定一种 flavor dimensions

    flavorDimensions "default"
      productFlavors {
        HuanStore {
          dimension "default"
        }
        huanhelper {
          dimension "default"
        }
        tvhuan {
          dimension "default"
        }
        dangbei {
          dimension "default"
        }
      }
    
      productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
      }
    

    获取meta-data的value

    //通过ApplicationInfo获取meta-data标签中指定name对应的value
    private String getMetaValue(String name) {
           String value = null;
            try {
                ApplicationInfo appInfo = this.getPackageManager()
                    .getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
            
                value = appInfo.metaData.getString(name);
                Log.d(TAG,name + "-->" + value);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return value;
    }
    
    通过ActivityInfo获取
    ActivityInfo info = this.getPackageManager()
                    .getActivityInfo(getComponentName(),PackageManager.GET_META_DATA);
      info.metaData.getString("meta_name");
    
    通过ServiceInfo获取
    ComponentName cn = new ComponentName(this, MetaDataService.class);
     ServiceInfo info = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA);
     info.metaData.getString("meta_name");
    
    通过ReceiverInfo获取
    ComponentName cn = new ComponentName(context, MetaDataReceiver.class);
    ReceiverInfo info = context.getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA);
    info.metaData.getString("meta_name");
    

    参考摘抄:http://blog.csdn.net/fengltxx/article/details/52824860

    相关文章

      网友评论

        本文标题:AndroidStudio多渠道打包build.gradle设置

        本文链接:https://www.haomeiwen.com/subject/lyzvbxtx.html