Multi-channel packaging by umeng and umeng

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.