Detailed explanation of Android using Meituan multi channel packaging scheme

  • 2021-11-13 18:17:20
  • OfStack

How fragmented is the Andorid channel market? Scattered to be more serious than Android fragmentation, are you still having a headache for multi-channel packaging? Meituan provides a multi-channel packaging scheme with a fast speed. It's a bit exaggerated. Yes, although it's exaggerated, it's really fast. It's not enough to describe it without exaggeration. Don't talk too much nonsense, first talk about the principle, then talk about the practical methods.

Comparison and explanation of the principles of new and old packing methods

Traditional way

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

Scenario 1: Complete recompilation, that is, before the code is recompiled and packaged, modify the channel label in AndroidManifest; Scheme 2: Unpack through ApkTool, then modify the channel label in AndroidManifest, and finally package and sign through ApkTool.

These two packaging methods, no matter which one, are very inefficient. Scheme 1 is inefficient, and the scale of packaging channels is very small. Scheme 2 is slightly more efficient, and the scale of packaging channels is OK, but these two schemes are surprisingly slow. If you try hundreds of channel packages, it is estimated that your computer can be stuck for 1 afternoon. Slow, of course, is also good. You don't have to work. It's nice to drink coffee and play with your mobile phone, isn't it? Haha …

Meituan's Efficient Multi-channel Packaging Scheme

Meituan's efficient multi-channel packaging scheme is to decompress an Android application package as an zip file package, and then find that an empty file is added to the directory where the signature is generated. The empty file is named after the channel name and does not need to be re-signed. This method does not need to re-sign, compile and other steps, which makes this method very efficient.

Step 1: Unzip the apk file

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

If you add an empty file in the META-INF directory, you don't need to re-sign the application. Therefore, by adding different empty files for different channel applications, only 1 can identify 1 channel.

Step 2: Add an empty channel file to the apk file using the python script

We use the python code to add an empty channel file to apk, with the channel name prefixed mtchannel_:


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

After adding the empty channel file to the directory, there is one more empty file named mtchannel_meituan in the META-INFO directory

Step 3: Read the channel name with java code and dynamically set the channel name

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

Method for java code to read channel name:


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 "";
    }
  }

After reading the channel name, we can set it dynamically. For example, the dynamic setting method of AU channel is: AnalyticsConfig. setChannel (getChannel (Context context)); That's good. In this way, only one apk needs to be copied for every channel package, and one empty file named with channel number can be added to META-INF. This packing method is very fast, and it is said that more than 900 channels can be finished in less than 1 minute. What I personally tested was that I used 10 seconds to play 32 channel packages, which was not very fast.

Practical use

You may say, I don't understand the python code above, and I don't understand the contents in that script. It doesn't matter. You can understand the principle carefully, because someone builds wheels for you, so we can ride them directly.

Practice method use

Step 1: Configure the python environment

Since we need to use script packaging, the corresponding computer must have an environment that can run python scripts. So our first step is to configure the python runtime environment.
Go to official website to download and install yourself, which is very simple. Official website address: https://www.python.org/

Step 2: Set up the python script and put the encapsulated class into the project
Good Samaritan has written the package script to run, and also encapsulated the entity tool class to read the channel number. You just need to go to github to download it.
Address: https://github.com/GavinCT/AndroidMultiChannelBuildTool
Of course, github also has related use introduction, which is very simple and easy to understand. Simply say here, there is an ChannelUtil. java class downloaded, which encapsulates the method of obtaining the channel number. You only need to call the setup code of Friends in the place where the application is started, such as AnalyticsConfig. setChannel (ChannelUtil. getChannel (this).

Step 3: Configure the channel list
After we downloaded the wheel on github, you unzip the file and edit the channel list in PythonTool/Info/channel. txt, without writing a channel name, just break the line.

Step 4: Copy the signed package and run the script
You have signed the package of apk file, copy to the PythonTool directory and MultiChannelBuildTool. py script level, directly double-click MultiChannelBuildTool. py to complete the package.

ok, basically finished here, talked about the principle, and talked about the practical way. In view of the fact that others have built wheels for you, it is very simple to use, so try it quickly.


Related articles: