SoFunction
Updated on 2025-04-07

Detailed explanation of the multi-channel package solution for Android using Meituan

How fragmented is the Andorid channel market? Dispersed to the point where it is more serious than Android fragmentation. Are you still having a headache about packaging through multiple channels? Meituan provides a multi-channel packaging solution that is as fast as possible. It's a bit exaggerated. Yes, although it's exaggerated, it's really fast. It's not enough to describe its speed. Without further ado, talk about the principles first, then talk about practical methods.

Comparative explanation of the principles of new and old packaging methods

Traditional way

In the era when AndroidManifest defined channels, multi-channel packaging was nothing more than the following two solutions:

  • Solution 1: Complete recompilation, that is, modify the channel mark in AndroidManifest before the code is recompiled and packaged;
  • Solution 2: Unpacking through ApkTool, then modify the channel mark in AndroidManifest, and finally package and sign through ApkTool.

No matter which package, these two methods of packaging are very low, and the solution is inefficient at all. Moreover, the packaged channel scale is very small. The second solution is slightly more efficient and the packaged channel scale is also OK. However, these two solutions are surprisingly slow. If you try to package hundreds of channels, your computer will probably be stuck for an afternoon. It is of course beneficial to slow down. You can stop working. It is also very comfortable to drink coffee and play with your phone while waiting slowly, right? Ha ha……

Meituan's efficient multi-channel packaging solution

Meituan's efficient multi-channel packaging solution is to decompress an Android application package as a zip file package, and then find that an empty file is added to the directory generated by the signature. The empty file is named with the channel name, and there is no need to re-sign. This method does not require re-signature, compilation and other steps, making this method very efficient.

Step 1: Unzip the apk file

We will directly decompress the apk, and the root directory after decompression will have a META-INF directory.

If you add an empty file in the META-INF directory, you do not need to re-sign the application. Therefore, by adding different empty files to applications from different channels, a channel can be uniquely identified.

Step 2: Use python script to add empty channel files to the apk file

We use python code to add empty channel files to apk, and the channel name is prefixed with mtchannel_:

import zipfile
zipped = (your_apk, 'a', zipfile.ZIP_DEFLATED) 
empty_channel_file = "META-INF/mtchannel_{channel}".format(channel=your_channel)
(your_empty_file, empty_channel_file)

After adding the empty channel file, the META-INFO directory has an empty file named mtchannel_meituan

Step 3: Use java code to read the channel name and dynamically set the channel name

After we generate the file with a script, the name of the file is named by the channel name, so when we start the program, we can use java code to dynamically read the channel name and set it dynamically.

How to read channel names by java code:

public static String getChannel(Context context) {
    ApplicationInfo appinfo = ();
    String sourceDir = ;
    String ret = "";
    ZipFile zipfile = null;
    try {
      zipfile = new ZipFile(sourceDir);
      Enumeration<?> entries = ();
      while (()) {
        ZipEntry entry = ((ZipEntry) ());
        String entryName = ();
        if (("mtchannel")) {
          ret = entryName;
          break;
        }
      }
    } catch (IOException e) {
      ();
    } finally {
      if (zipfile != null) {
        try {
          ();
        } catch (IOException e) {
          ();
        }
      }
    }

    String[] split = ("_");
    if (split != null &&  >= 2) {
      return (split[0].length() + 1);

    } else {
      return "";
    }
  }

After reading the channel name, we can set it dynamically. For example, the dynamic setting method of Youmeng channel is: (getChannel(Context context)); that's fine. In this way, you only need to copy an apk for each channel package and add an empty file named with the channel number in META-INF. This packaging method is very fast, and it is said that more than 900 channels can be completed in less than a minute. My personal test was that I used 10 seconds to call 32 channel packages, is it very fast?

Practical use

You may say that I can't understand the python code above, and I can't understand the content in that script, it doesn't matter. Just understand the principle carefully, because someone makes wheels for you, so we can just ride them.

Practical method use

Step 1: Configure the Python environment

Since we need to use scripts to package, there must be an operating environment on the corresponding computer that can run python scripts. So our first step is to configure the python running environment.
Just download and install it yourself, it's very simple. Official website address:/

Step 2: Set up the python script and put the encapsulated classes into the project
The kind-hearted person has written the running package script and also encapsulates the entity tool class that reads the channel number. Everyone just needs to download it on github.
address:/GavinCT/AndroidMultiChannelBuildTool
Of course, there are also related introductions on github. It is very simple and you can understand it at a glance. Let me briefly talk about it here. There is a class that has been packaged in it. You only need to call the setting code of Youmeng at the place where you start the application, such as: ((this)).

Step 3: Configure the channel list
After we download the wheel on github, you unzip the file and edit the channel list in PythonTool/Info/. If you don’t write a channel name, just wrap it.

Step 4: Copy the signed package and run the script
You copy the apk file you have signed and packaged to the PythonTool directory and the same level as this script, and you can complete the packaging by double-clicking and clicking.

OK, I basically finished it here. I talked about the principles and practice methods. Since others have made wheels for you, it is very simple to use, so I will try it quickly.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.