第一步:在清单文件AndroidManifest.xml中application模块中加入:
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}"/>
第二步:在app的build.gradle中的android部分加入:
flavorDimensions ""
productFlavors{
wandoujia{}
xiaomi{}
huaiwei{}
oppo{}
vivo{}
meizu{}
yingyongbao{}
}
productFlavors.all{
flavor ->
flavor.manifestPlaceholders =[UMENG_CHANNEL_VALUE : name]
}
applicationVariants.all{ variant ->
variant.outputs.all{ output->
def outputFile = output.outputFile
if(outputFile!=null && outputFile.name.endsWith(".apk")){
def fileName = outputFile.name.replace(".apk","-${defaultConfig.versionName}.apk")
outputFileName = fileName
}
}
}
第三步:在示例ApplicationTest(在任何地方都可以)中获取meta-data
private String getChannel(){
PackageManager pm = getPackageManager();
ApplicationInfo applicationInfo = null;
try {
applicationInfo = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
return applicationInfo.metaData.getString("UMENG_CHANNEL");
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return "";
}
第四步:build ->Generate Signed Bundle or apk -> apk,打包apk
第五步:验证apk的渠道信息,