Review images

How fragmented is the Android channel market? Fragmentation is worse than Android fragmentation, are you still struggling with multi-channel packaging? Meituan provides a fast multi-channel packaging scheme. That’s an exaggeration. Yeah, it’s an exaggeration, but it’s fast. It’s not an exaggeration. Don’t talk nonsense, first talk about the principle, and then talk about practical methods.

New and old packing method principle comparison explanation

The traditional way

In the days when AndroidManifest defined channels, multi-channel packaging can be done in one of two ways:

  • Option 1: complete recompile, that is, before the code is recompiled and packaged, modify the channel identifier in AndroidManifest;

  • Scheme 2: Unpack the package through ApkTool, modify the channel identifier in AndroidManifest, and finally package and sign the package through ApkTool.

These two kinds of packaging, either, the efficiency is very low, plan an ineffective, and packaging of channel size is very small, the second a few high solution efficiency and packaged channel size can also, but the two scheme is slow, if you make a try hundreds of channel package, can estimate your computer card one afternoon. Slow, of course, has its advantages, you can not work, drinking coffee, playing with the mobile phone to wait for the comfortable, right? Ha ha…

Meituan efficient multi-channel packaging solutions

Meituan’s efficient multi-channel packaging solution is to unpack an Android application package as a ZIP file and find that an empty file is added to the directory generated by the signature. The empty file is named by the channel name and does not need to be re-signed. This approach requires no steps such as re-signing, compiling, etc., making it very efficient.

Step 1: Decompress the APK file

Decompress apk directly and the root directory will have a meta-INF directory as shown below:

Review images

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

Step 2: Add the empty channel file to the APK file with a Python script

We use Python code to add an empty channel file to APK with the channel name prefixed by 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)Copy the code

An empty file named mtchannel_meituan is added to the meta-info directory after the empty channel file is added:

Review images

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

Java code reads the 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 ""; }}Copy the code

Read the channel name, we can dynamically set, such as their Allies channels dynamically setting method is: AnalyticsConfig. SetChannel (getChannel (Context Context)); That’s it. In this way, for each channel package, just copy an APK and add an empty file named with the channel number in meta-INF. This packaging method is very fast, it is said that more than 900 channels can be completed in less than a minute. My personal test is that I played 32 channels in 10 seconds, is not fast.

Practical use

You might say, I don’t understand the Python code above, I don’t understand what’s in that script, and that’s okay. You just need to understand the principle carefully, because someone built you a wheel, we can just ride it.

Use of practice methods

Step 1: Configure the Python environment

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

Step 2: Set up the Python script and put the wrapped classes into the project

The good people have already written the package script to run, and also wrapped the entity utility class to read the channel number. All you have to do is go to Github and download it. Address: github.com/GavinCT/And… Of course, github also has the related use of the introduction, very simple, a look at the understanding. For a brief introduction, there is a channelUtil. Java class that encapsulates the method to get the channel number. All you need to do is call the Channel Settings code where the application is launched, for example: AnalyticsConfig. SetChannel (ChannelUtil. GetChannel (this)).

Step 3: Configure the channel list

After we download the wheels on the lot, you unzip the files, in PythonTool/Info/channel. TXT edit the channel list, didn’t write a channel name, can be a new line.

Step 4: Copy the signed package and run the script

You copy your signed and packaged APk files to the PythonTool directory at the same level as the multichannelBuildTool. py script. Double-click multichannelBuildTool. py to complete the package.

Ok, here is basically finished, talked about the principle, and talked about the practice, in view of others have made a good wheel for you, so it is very simple to use, hurry to try it. If you don’t understand can leave a message, welcome to exchange.

Recommended articles:

Android Studio uses Gradle multichannel packaging

Reference article: tech.meituan.com/mt-apk-pack…


Review images