Multi-channel packaging by umeng and umeng
1. steps:
1. According to the requirements of umeng, the manifest file must contain
<meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_VALUE}" />
In this configuration, the value is the channel name such as wandoujia and 360, but we will not write the channel name here, but it is a placeholder, later, gradle will be replaced dynamically during compilation.
2. Add the following content to the android {} of build. gradle of module (generally the app:
productFlavors{ wandoujia{ manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"] } xiaomi{ manifestPlaceholders=[UMENG_CHANNEL_VALUE: "xiaomi"] } }
ProductFlavors is a self-node of the android node. For the channel package you need, assign a value to UMENG_CHANNEL_VALUE according to umeng's requirements.
3. Optimization 1: The above are only two channels. If there are dozens of channels that are written in this way and there are too many repeated items, we can see that each channel is the name of flavor, so we can modify it as follows:
productFlavors{ wandoujia{ //manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"] } xiaomi{ //manifestPlaceholders=[UMENG_CHANNEL_VALUE: "xiaomi"] } } productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] }
3. optimization 2: The name of the apk generated after the signature is packaged above has a default naming rule, such as: xxx-xiaomi-release.apk but we want to include version information such as: xxx-xiaomi-release-1.0.apk, so the final packaging script is as follows:
productFlavors{ wandoujia{ //manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"] } xiaomi{ //manifestPlaceholders=[UMENG_CHANNEL_VALUE: "xiaomi"] } } productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] } applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { def fileName = outputFile.name.replace(".apk", "-${defaultConfig.versionName}.apk") output.outputFile = new File(outputFile.parent, fileName) } } }
4. Obtain Channels
In the code, we can obtain the channel by reading the mate-data information and add it to the request parameters. The method is as follows:
private String getChannel() { try { PackageManager pm = getPackageManager(); ApplicationInfo appInfo = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); return appInfo.metaData.getString("UMENG_CHANNEL"); } catch (PackageManager.NameNotFoundException ignored) { } return "";}
5. Execute signature packaging:
At this time, you can go to app/build/outputs/apk to see the channel package that is automatically prepared.
Ii. Disadvantages:
This packaging method is inefficient. If dozens of packages can be used, it takes dozens of seconds to create a package. If it is slow, it may take several minutes, which is highly related to machine performance.