Detailed explanation of Android using meituan multi-channel packaging scheme

How fragmented is the andorid channel market? It's more fragmented than Android. Are you still having a headache for multi-channel packaging? Meituan provides a fast multi-channel packaging scheme. It's a little exaggerated. Yes, although exaggerated, it's really fast. No exaggeration is not enough to describe its speed. Don't talk too much nonsense. First talk about principles, then practical methods.

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

Traditional way

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

Either of these two packaging methods is inefficient. Scheme 1 is inefficient, and the packaging channel scale is very small. The efficiency of the second scheme is slightly higher, and the packaging channel scale is also OK. However, the speed of these two schemes is amazing. If you try hundreds of channel packages, it is estimated that your computer can card all 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? Ha ha

Meituan's efficient multi-channel packaging scheme

Meituan's efficient multi-channel packaging scheme is to unzip 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 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 directly unzip APK, and there will be a meta inf directory in the root directory after unzipping

If you add an empty file in the meta inf directory, you can apply without re signing. Therefore, a channel can be uniquely identified by adding different empty files to applications from different channels.

Step 2: add an empty channel file to the APK file with a python script

We use Python code to add an empty channel file to APK. The prefix of the channel name is 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, there is one more directory named mtchannel in the meta info directory_ Empty file for meituan

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

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

How to read the channel name by 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 "";
    }
  }

After reading the channel name, we can set it dynamically. For example, the dynamic setting method of Youmeng channel is: analyticsconfig. Setchannel (getchannel (context)); That's good. 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 the meta inf. This packaging method is very fast. It is said that more than 900 channels can be completed in less than a minute. I personally tested that I used 10 seconds to hit 32 channel packages. Is it fast.

Practical use

You might say, I can't understand the Python code above, and I can't understand the contents of the script. It doesn't matter. You can understand the principle carefully, because someone makes wheels for you, and we can ride them directly.

Use of practical methods

Step 1: configure the python environment

Since we need to use script packaging, there must be a running environment on the corresponding computer that can run Python scripts. So our first step is to configure the python running environment. You can download and install it on the official website. It's very simple. Official website address: https://www.python.org/

Step 2: set the python script and put the encapsulated class into the project. The kind-hearted person has written the running packaged script and encapsulated the entity tool class for reading the channel number. You just need to download it from GitHub. Address: https://github.com/GavinCT/AndroidMultiChannelBuildTool Of course, there is also a related introduction on GitHub, which is very simple and easy to understand. To put it simply, there is a channelutil.java class downloaded, which encapsulates the method of obtaining the channel number. You only need to call the setting code of Youmeng at the place where you start the application, such as analyticsconfig.setchannel (channelutil. Getchannel (this)).

Step 3: configure the channel list. After we download the wheel on GitHub, you unzip the file, edit the channel list in Python tool / Info / channel.txt, and wrap the line without writing a channel name.

Step 4: copy the signed package and run the script. You can copy the APK file you have signed and packaged to the directory of Python tool and the same level as the script multichannelbuildtool.py. Double click multichannelbuildtool.py directly to complete the packaging.

OK, I've basically finished here. I've talked about the principle and the way of practice. Since others have built wheels for you, it's very easy to use. Go and have a try.

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>