赞
踩
ONE Goal ,ONE Passion !
Studio打包步骤详解:
http://blog.csdn.net/fengltxx/article/details/52816199
<meta-data
android:name="UMENG_CHANNEL"
android:value="${Pack_CHANNEL}" />
build.gradle中:
productFlavors {
wandoujia {}
youmeng {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [Pack_CHANNEL: name]
}
注意:flavor.manifestPlaceholders = [Pack_CHANNEL: name]中的Pack_CHANNEL 一定要和 AndroidManifext.xml 定义的android:value=”${Pack_CHANNEL}”一致
如果不一致会报错:
心中冒出几个想法.
现在name=”UMENG_CHANNEL” 写成了UMENG_CHANNEL.可以理解为我们集成了UMENG. 友盟可以根据我们的这个name=”UMENG_CHANNEL”配置获取对用的value的值.
当打包时.会根据productFlavors中的每一个值去替换value中的值
如果打包两次.每个apk中分别为:
wandoujia.apk:
<meta-data
android:name="UMENG_CHANNEL"
android:value="wandoujia" />
youmeng.apk
<meta-data
android:name="UMENG_CHANNEL"
android:value="youmeng" />
meta-data类似与map集合存储的键值对.每个name对用一个value.
meta-data可以配置在清单文件中Application,Activity,service,receiver中.获取方式大致雷同.
Application为例:
package com.example.retrifitdemo.applicaction;
import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
/**
* ONE Goal,ONE Passion!
* Created by ${PK_Night} on 2016/10/14.
* comment:
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//在application应用<meta-data>元素。
ApplicationInfo appInfo = null;
try {
appInfo = this.getPackageManager()
.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
String umeng_channel = appInfo.metaData.getString("UMENG_CHANNEL");
System.out.println("--umeng_channel--" + umeng_channel);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}
Activity获取元素
ActivityInfo info = this.getPackageManager()
.getActivityInfo(getComponentName(),PackageManager.GET_META_DATA);
info.metaData.getString("meta_name");
service获取元素
ComponentName cn = new ComponentName(this, MetaDataService.class);
ServiceInfo info = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA);
info.metaData.getString("meta_name");
receiver获取元素
ComponentName cn = new ComponentName(context, MetaDataReceiver.class);
ActivityInfo info = context.getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA);
info.metaData.getString("meta_name");
当集成友盟后,如果是豌豆荚市场发布的apk出现了问题.那么 apk中代码获得的UMENG对应的value就为wandoujia 这样就能统计出是哪个市场的信息了.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。