Build配置
在android{}内加
productFlavors {
sanxing {
manifestPlaceholders = [CHANNEL_VALUE: "32d2sss"]
}
guanwang {
manifestPlaceholders = [CHANNEL_VALUE: "\\00000000"]
}
}
AS 3.0+需在 defaultConfig {}内配置
flavorDimensions "default"//写其他的也行
manifest里的配置
<meta-data
android:name="DEMO_CHANNEL"
android:value="${CHANNEL_VALUE}"
></meta-data>
代码中获取渠道号
try {
PackageManager packageManager = ctx.getPackageManager();
if (packageManager != null) {
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
if (applicationInfo != null) {
if (applicationInfo.metaData != null) {
String channel = applicationInfo.metaData.get("DEMO_CHANNEL")+"";
AppConfig.CHANNEL = channel;
LogService.i("Channel","当前的渠道为:"+channel );
}
}
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
LogService.e("Channel",e.getMessage()+"");
}
转义问题
如果你的渠道号是一堆0000001 那么最后获取出来就是1
如果想获取到位000001 那么就需要加转义字符\0000001