Several common multi-channel (batch) packaging methods for Android

Source: Internet
Author: User

Several common multi-channel (batch) packaging methods for Android

Multi-channel packaging is mainly used to count the number of packages downloaded from different channels. The more channels, the more packages we need. At this time, we cannot use manual packaging to generate different channel packages one by one. We need a more efficient packaging method.

Method 1 of declaring a channel:

Generally, we use the following method in the application of our manifest file to define channel information:



In our Java code, we can use the following method to obtain the channel number:



/*** Get Umeng channel number ** @ param context * @ return channel number */public static String getChannel (Context context) {String channel = "bbliveBYD "; try {ApplicationInfo info = context. getPackageManager (). getApplicationInfo (context. getPackageName (), PackageManager. GET_META_DATA); if (info! = Null & info. metaData! = Null) {String metaData = info. metaData. getString ("UMENG_CHANNEL"); if (! StringUtil. isBlank (metaData) {channel = metaData ;}} catch (NameNotFoundException e) {e. printStackTrace ();} return channel ;}

You can use the following packaging methods to Declare Channel numbers:


1. umeng packaging tools:

The usage is relatively simple. First, create a signature package in the normal way, and then open the downloaded packaging tool. We recommend that you use the latest v3.2 (fixed some bugs in earlier versions)

Click the wrench to configure the application signature.

At the bottom, give a name to my configuration file. Save the settings.

Next, drag the signature package we used in the previous step into the plus sign of the umeng packaging tool. After the analysis is complete, click one-click packaging. If you have any problems with this method, you can refer to the instructions above in git.


2. Pack the blue shell Eclipse plug-in.

There is also a packaging method, which is put in Eclipse in the form of plug-ins, called lanbei shell. It is a jar package and is quite convenient to use. In early companies, when using multiple channels, I also use this method. The advantage is that after installing the plug-in, you only need simple configuration to package the plug-in batches.

Download a kb Eclipse plug-in.


Android development auxiliary tool-guide to using the "lanbei shell" Eclipse plug-in

1. Download The lanbei shell Eclipse plug-in

2. Install the lanbei shell plug-in to build an Android Project

3. Use the configuration file and default configuration file

4. Use the "open in resource manager" Function

5. Use the "dependency Update" Function

6. Use the "package and release" Function

1. Download The lanbei shell Eclipse plug-in



Download and decompress orchidshell.rar


Confirm that the folder path is OrchidShell --- eclipse --- plugins


2. Install the lanbei shell plug-in to build an Android Project

Install the Eclipse plug-in, copy the OrchidShell folder to the dropins folder of Eclipse, and start (restart) Eclipse


Create an Android Project


Right-click the project and choose lanbei shell from the shortcut menu. The installation is successful.


3. Use the "open in resource manager" Function

Click Open in resource manager in the lanbei shell menu to open the folder where the selected resource is located.

If the selected resource is a folder, the plug-in will open the folder directly.

If the selected resource is a file, the plug-in will open the parent folder of the file and select the file


4. Use the configuration file and default configuration file

Lanbei shell uses xml as the plug-in configuration file. When there is no configuration file in the project and some functions of the plug-in need to use the configuration file, the plug-in will create a default configuration file.

This configuration file references a general Android development kit. A keystore for signature is configured. You need to modify the configuration file according to your actual situation. The following two functions are described in detail.

5. Use the "dependency Update" Function


Right-click the "dependency Update" menu in the plug-in to update and manage third-party dependent jar packages of the project.


Third-party jar packages used here, use maven to manage the jar package organization form


It should be noted that the jar package here must have a unified download website. This website can be any maven library or maven library image. If you have a Team server, you can also create a Maven library to manage common components.


After you click the menu, the update process is displayed in the Eclipse console.

Note: If the configuration file is incorrectly configured, some error messages may be reported. modify the configuration file as prompted.


6. Use the "package and release" Function

Click the "package and release" menu in the lanbei shell menu to package the project separately by channel. During the packaging process, the apk file can be automatically signed.


Note: before packaging and publishing, You need to modify the corresponding project in the configuration file:

The keyname = "" of channels refers to the Application-level configuration in your AndroidManifest. xml file.. For example:


Here, UMENG_CHANNEL and gfan_cpid are used by UMENG_CHANNEL and flink. You can modify the channel project identifier of the statistics SDK you have applied, if there are multiple, separate them with commas.


Is the name of each channel you want to package, that is, the channel identifier in the statistics SDK. For each channel written here, the plug-in will automatically modify the value in meta-data for it and package it into an apk file. Then sign the apk file.



Here, the keystore is the personal key you use to sign the apk file. You need to change the path to your keystore path, and passwd is the keystore password.

The following alias refers to the alias and password corresponding to each key. If you have multiple alias, you can specify them here. The plug-in selects the first available alias and the Key corresponding to the password to sign your apk file.


This path is the storage path after your apk file is automatically packaged and generated. Note that the plug-in will create a folder named after your project in this path.


Instructions & plug-in downloads

3. Gradle multi-channel packaging, relatively new and easy to use


Android studio has not been used to create a signature package. Therefore, the signature generation method is first pasted:


Click Build in Android Studio and select generate Signed APK.

Select create new key store:

J. Click OK.

Next, select the output apk location.


Next, let's talk about gradle multi-channel packaging.

1. Define placeholders in AndroidManifest. xml

Android: name = "UMENG_CHANNEL"
Android: value = "$ {UMENG_CHANNEL_VALUE}"/>

2. Replace the placeholder in the module's gradle script

ProductFlavors {
Wangdoujia {
ManifestPlaceholders = [UMENG_CHANNEL_VALUE: "wangdoujia"]
ManifestPlaceholders = [UMENG_CHANNEL_VALUE: ""]
Anzhi {
ManifestPlaceholders = [UMENG_CHANNEL_VALUE: "anzhi"]


If you use more channels, add them in the preceding format. After adding them, click build -----> generate signed apk.


Statement channel Method 2: Android bulk packaging speed up-900 markets per minute is not a dream

The last packaging method also has a lot of information on the Internet, that is, the packaging tool based on the packaging principle of Meituan.

See this article.

Android bulk packaging speed-900 markets per minute is not a dream


The normal packaging method takes a long time to modify the manifest file.

Android: name = "UMENG_CHANNEL"
Android: value = "$ {UMENG_CHANNEL_VALUE}"/>


And then continue the signature, so it takes time.


The packaging ideas provided by Meituan are as follows: (Android automation tour of the original Meituan-channel package generation)


If you can directly modify the channel number of the apk without re-signing, you can save a lot of packaging time. Fortunately, we found this method. Decompress the apk directly. The root directory after decompression has a META-INF directory, as shown in:


If you add an empty file to the META-INF directory, you do not have to sign the application again. Therefore, you can add different Empty files to applications of different channels to uniquely identify a channel.



Directory after adding an empty channel file,META-INFOA directory namedmtchannel_meituanEmpty file:

Next we can read the empty channel file name in the Java code:

public static String getChannel(Context context) {        ApplicationInfo appinfo = context.getApplicationInfo();        String sourceDir = appinfo.sourceDir;        String ret = "";        ZipFile zipfile = null;        try {            zipfile = new ZipFile(sourceDir);            Enumeration entries = zipfile.entries();            while (entries.hasMoreElements()) {                ZipEntry entry = ((ZipEntry) entries.nextElement());                String entryName = entry.getName();                if (entryName.startsWith("mtchannel")) {                    ret = entryName;                    break;                }            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (zipfile != null) {                try {                    zipfile.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        String[] split = ret.split("_");        if (split != null && split.length >= 2) {            return ret.substring(split[0].length() + 1);        } else {            return "";        }    }

In this way, you only need to copy an apk for each channel package.META-INFAdd an empty file named by channel number. This packaging method is very fast, and more than 900 channels can be completed in less than one minute.

According to the ideas provided by the US Mission, add an empty file in the META-INFO as a channel, can meet our channel requirements, so do not need to sign, it greatly reduces the packaging time

ThereforeAndroid bulk package acceleration-900 markets per minute is not a dream article.


The procedure is as follows:


Download python:
Download others written script:

#### Procedure

1. Copy 'channelutil. Java' to the project.
2. Use the ChannelUtil. getChannel (this) method in the code to obtain the channel number.
3. Install Python.
4. '.. PythonTool \ info \ channel.txt' to add the desired channel.
5. Place the apk in the directory of 'multichannelbuildtool. py' and double-click 'multichannelbuildtool. py' to execute the script to complete multi-channel packaging.

The following shows how to package



I have configured 65 packages here and it will be finished in an instant.

The channel configuration is also very convenient. You can configure it in special channel.txt.




Comparison of various packaging methods:

In terms of speed, the last method is invincible and fast, and it is very convenient to use. You only need to manually create a signature package and then execute the script.


The Umeng packaging tool can also be a normal package, but if there are too many channels, the configuration is troublesome, And the apk packaging containing the so dynamic link library may fail.


Lanbei shell Eclipse packaging plug-in, I used to use the packaging method, the speed is slower than the packaging idea of Meituan, but generally there is no error.



Data is collected and stored on the Internet. If you have good ideas, you can share them with us.



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: 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.