In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use Meituan's multi-channel packaging solution in Android. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
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:
Scheme 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 tag in AndroidManifest, and finally package and sign through ApkTool.
No matter which one is, the efficiency of these two kinds of packaging is very low, the first solution has no efficiency to speak of, and the scale of the packing channel is very small, and the second scheme is slightly more efficient, and the scale of the packing channel is OK, but the speed of these two schemes is astonishingly slow. If you try hundreds of channel packages, it is estimated that your computer will be stuck all afternoon. Slow, of course, there are benefits, you can not work, drinking coffee, playing with the phone to wait is also very comfortable, right? Ha ha.
Meituan's efficient multi-channel packaging scheme
Meituan's efficient multi-channel packaging scheme is to extract an Android application package as a zip package, and then find that an empty file is added under the directory generated by the signature. The empty file is named after the channel name, and there is no need to re-sign. This method does not require re-signature, compilation and other steps, which makes this method very efficient.
Step 1: extract the apk file
We decompress apk directly, and the decompressed root directory will have a META-INF directory.
If you add an empty file in the META-INF directory, you don't have to re-sign the application. Therefore, a channel can be uniquely identified by adding different empty files for applications in different channels.
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 with mtchannel_:
Import zipfilezipped = zipfile.ZipFile (your_apk, 'channel, 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 an empty file named mtchannel_meituan in the META-INFO directory.
Step 3: read the channel name with java code and set the channel name dynamically
After we use the script to generate the file, 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.
The 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.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 context)); this is good. In this way, you only need to copy one apk for each dozen channel packages, and add an empty file named using the channel number in the META-INF. This kind of packing speed is very fast, it is said that more than 900 channels can be finished in less than a minute. What I personally tested is that it took me 10 seconds to hit 32 channel packs, whether it is very fast.
Practical use
You might say, I don't understand the python code above, I don't understand what's in that script, it doesn't matter. You can understand the principle carefully, because someone built a wheel for you, and we can just ride it.
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 runtime environment. Go to the official website to download and install it, it's very simple. Official website address: https://www.python.org/
Step 2: set up the python script and put the encapsulated class in the project. The kind-hearted person has written the running packaging script and encapsulated the entity tool class to read the channel number. All you have to do is download it on github. Address: of course, https://github.com/GavinCT/AndroidMultiChannelBuildTool also has related usage introduction on github, which is very simple and easy to understand. To put it simply, you can download a ChannelUtil.java class that encapsulates the method of getting the channel number. You only need to call Youmian's setting code 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 PythonTool/Info/channel.txt, and change the line without writing a channel name.
Step 4: copy the signed package, run the script you have signed and packaged apk file, copy to the PythonTool directory and MultiChannelBuildTool.py this script at the same level, directly double-click MultiChannelBuildTool.py to complete the package.
Thank you for reading! This is the end of this article on "how to use Meituan's multi-channel packaging plan in Android". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 248
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.