Android Multi-Channel packaging solution

Source: Internet
Author: User
Tags file copy

General Build

Let's take a look back. Multi-channel bulk packaging via ant or Gradle, typically configured in Androidmanifest:

<meta-data android:name="CHANNEL" android:value="xxx" />

Meta-data dynamically changes the channel name by configuring value, and then we can get channel in this code

privategetChannelNameFromManifest(){try {        return (String) mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(), PackageManager.GET_META_DATA).metaData.get("CHANNEL");    catch (NameNotFoundException e) {        e.printStackTrace();    }}

Ant is a bit more cumbersome to configure on multi-channel packages, so if you're interested, you can refer to this blog's humble world-"app Automation uses ant compilation projects for multi-channel packaging."
and Gradle as the official Android build tool, bulk packaging nature will be relatively simple, like the following simple configuration can be:

/** * ------------------------------------------------------- * Flavors * -------------------------------------------------------- */productFlavors {    intl {        "com.smartphone.linux1s1s.international"        versionName createVersionName(versionNameInit)        "nlbtn2goint"]    }    gloav {        "com.smartphone.linux1s1s.android"        versionName createVersionName(versionNameInit)        "nlbtn2go"]    }}

Configuration can be so simple, but bulk packaging or around the new build this process, in a popular word is, if I need 1000 channel package, each channel package needs 5 minutes, then batch complete these packages takes 5,000 minutes, Android in the development of the gradle may be completely unexpected this problem, because in addition to China has so many channels market, other countries are the same, after a few words, we still have to face the reality, solve this time-consuming packaging problems.

Here, from the simplest case, if so many channels are only to distinguish between different channel, the others are the same. If there are any other nuances, it is not considered here, but even the nuances may be just individual channels, so the dumbest solution is to build these individual channels from the new one, and the rest can be solved by a shortcut.

What is the shortcut, of course, to avoid a new build, if not new, if you do distinguish between different channels?

Optimize build

Let's look at the APK structure first

The files in the Meta-inf directory in this compressed directory are not signed in principle, so we can avoid the new build by putting the channel flag in the file, so that we can do the bulk packing directly through the file operation. and the file copy decompression compression operation time and compared to the time spent from the new construction, it is negligible, above is the core idea of the whole package, then we will implement the above core ideas.

Actual operation

Add an empty file named Xxx_channelname_channelid to the Meta-inf directory, with an example of Xiaomi channels as shown below:

Then unzip and add the files to the directory to the. apk format, the above work is done, the next step is to bulk add the file process, here is a Python script for reference

Python script
#!/usr/bin/python# Coding=utf-8ImportZipFileImportShutilImportOs# Empty file facilitates writing this empty file into the APK package as a channel fileSrc_empty_file =' Xxxxx.txt '#创建一个空文件 (created if not present) withOpen (Src_empty_file,' W ') asF:Pass# Get all the APK source packages in the current directorySrc_apks = []# Python3:os.listdir (), use Python2 compatible Os.listdir ('. ') forFileinchOs.listdir ('. '):ifOs.path.isfile (file): Extension = os.path.splitext (file) [1][1:]ifExtensioninch ' apk ': Src_apks.append (file)# Get a list of channelsChannel_file =' ChannelConfig.txt ' withOpen (Channel_file) asF: forsrc_apkinchSrc_apks:# file name (with extension)Src_apk_file_name = Os.path.basename (src_apk)# split file name and suffixTemp_list = Os.path.splitext (src_apk_file_name)# name without extensionSrc_apk_name = temp_list[0]# suffix name, including. For example: ". apk"Src_apk_extension = temp_list[1]# Create a build directory, associated with the file nameOutput_dir =' Output_ '+ Src_apk_name +'/'        # Directory does not exist then create        if  notOs.path.exists (Output_dir): Os.mkdir (Output_dir)# Iterate through the channel number and create the APK file corresponding to the channel number         forLineinchF.readlines ():# Get the current channel number, as you get it from the channel file with \ n, all stripTarget_channel = Line.strip ()# Get channel number, isolate channel name and channel ID fromTarget_channel_split = Target_channel.split (' _ ') Target_channel_name = target_channel_split[0]#target_channel_id = target_channel_split[1]            # match the channel number apktarget_apk = Output_dir + Src_apk_name +'-release-'+ Target_channel_name + src_apk_extension# Copy to create a new apkShutil.copy (src_apk, target_apk)# Zip Gets the newly created APK filezipped = ZipFile. ZipFile (target_apk,' A ', ZipFile. zip_deflated)# Initialize Channel informationEmpty_channel_file ="Meta-inf/xxxx_{channel}". Format (channel = Target_channel)# write Channel informationZipped.write (Src_empty_file, Empty_channel_file)# Close the zip streamZipped.close ()

If you have any questions, please feel free to communicate.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Multi-Channel packaging solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.