Androidstudio Multi-appid multichannel Quick pack

Source: Internet
Author: User

Always feel Androidstudio not eclipse fast, but recently because of a problem had to move the project to Androidstudio, all the bulk packaging that was done on eclipse before the migration had to be re-run on Androidstudio again, I have to say that this process is far more enjoyable than I thought, Androidstudio's strong and useful changed my prejudice to this IDE, and undoubtedly this IDE is the best tool to develop Android.

One, ordinary packaging configuration
    defaultConfig {        applicationId myAppId        14        19        versionCode vCode        versionName vName        "com.xx.xxx.test"        "android.test.InstrumentationTestRunner"        true        signingConfig signingConfigs.releaseConfig        manifestPlaceholders = [package_name: myAppId, app_icon: myAppIcon,                                app_name: myAppName, umeng_appkey: umengAppKey,                                main_menu_json: mainMenus, main_app_change_string:appChangedText]    }

This configuration is a default configuration item, we can configure some common things here, here can see I AppId (that is, the usual name of the package) with a variable to configure, because in my project the same source to play multiple applications (APPID), such requirements should be rare, But someday you'll meet

def ‘com.xx.xxx‘

I'm sure you'll notice the bottom of my configuration.

manifestPlaceholders = [package_name: myAppId, app_icon: myAppIcon,                                app_name: myAppName, umeng_appkey: umengAppKey,                                main_menu_json: mainMenus, main_app_change_string:appChangedText]

These configurations are variables defined in the Mainfest file, where a unified configuration is done (because it is the default configuration), and if individual channels need to be configured individually, you can do a specific personalization in the productflavors below.

   {       forum { }   }

This is the official website package configuration (using the default configuration), so do not need to do a personalized configuration, so it is OK, if you want to add other channels, such as Baidu to personalize the application name.

   productFlavors {       forum { }       baidu{           ‘百度‘]       }   }

This seems to be perfect is not, can not reach the channel different AppID package, in fact, it is true, but we all know Android application market good hundreds of, generally last more than 20 application market (that is, hit 20 channels) is not much, this time you may be going to cry out, You can go to the bathroom several times in a bag and even have a dinner. Androidstudio build time is quite long, this presumably used people have this feeling, since so we can learn from eclipse on the multi-channel packaging ideas, hit a package other package by modifying the configuration file implementation? In fact, people have done this practice and proved to be effective.

Second, the rapid out of the package channel no more afraid

If you can directly modify the APK channel number, and do not need to re-sign can save a lot of packaging time. Fortunately, we have found this way. Directly unzip the apk, the extracted root directory will have a meta-inf directory, if you add empty files within the Meta-inf directory, you can not re-sign the application. Therefore, you can uniquely identify a channel by adding different empty files for different channels of application. The idea is actually very simple (please refer to this article for details: 1190000003763833)

    /** * Get version information from APK * @param context * @param channelkey * @return */    Private StaticStringgetchannelfromapk(context context, String Channelkey) {//Get from APK packageApplicationInfo appinfo = Context.getapplicationinfo (); String SourceDir = Appinfo.sourcedir;//default in meta-inf/, so you need to stitch it up again.String key ="meta-inf/"+ Channelkey; String ret =""; ZipFile ZipFile =NULL;Try{ZipFile =NewZipFile (SourceDir); enumeration<?> entries = Zipfile.entries (); while(Entries.hasmoreelements ())                {ZipEntry entry = ((ZipEntry) entries.nextelement ()); String entryName = Entry.getname ();if(Entryname.startswith (key)) {ret = EntryName; Break; }            }        }Catch(IOException e)        {E.printstacktrace (); }finally{if(ZipFile! =NULL) {Try{Zipfile.close (); }Catch(IOException e)                {E.printstacktrace (); }}} string[] split = Ret.split ("_"); String channel ="";if(Split! =NULL&& Split.length >=2) {channel = Ret.substring (split[0].length () +1); }returnChannel }

This method is to get the name of our channel from the file name (different packages will be decompressed after the creation of a relevant channel name of the file), how to create channel-related files after decompression? We use the following Python script implementation.

#!/usr/bin/python# Coding=utf-8Import Zipfileimport shutilimport OS# Empty file facilitates writing this empty file into the APK package as a channel fileSrc_empty_file =' Info/czt.txt '# Create an empty file (created if not present)f =Open(Src_empty_file,' W ') F.Close()# Get all the APK source packages in the current directorySrc_apks = []# Python3:os.listdir (), use Python2 compatible Os.listdir ('. ')Apk_path =' e:/androidstudio/work/xxx/app/build/outputs/apk/' for file inchOs.listdir (apk_path): Fulldirfile = Os.path.join (Apk_path,file)ifOs.path.isfile (fulldirfile): extension = Os.path.splitext (file)[1][1:]ifExtensioninch ' apk ':if file. Find (' unaligned ') == -1: Src_apks.append (fulldirfile) print (SRC_APKS)# Get a list of channelsChannel_file =' Info/channel.txt 'f =Open(Channel_file)Lines= F.readlines () F.Close() 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 namePackage_name = src_apk_name[0: Src_apk_name.index ('-')] Output_dir =' g:/xxx pack/'+ Package_name +'/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     for  Line inch Lines:# Get the current channel number, as you get it from the channel file with \ n, all stripTarget_channel = Line. Strip ()# match the channel number apktarget_apk = Output_dir + Src_apk_name +"-"+ Target_channel + 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/cztchannel_{channel}".format(channel = Target_channel)# write Channel informationZipped.Write(Src_empty_file, Empty_channel_file)# Close the zip streamZipped.Close()

Here I modify the original GitHub code based on my actual work, some of which are changed to absolute path, and the unaligned packet is filtered.

‘E:/AndroidStudio/work/xxx/app/build/outputs/apk/‘
iffile.find(‘unaligned‘) == -1:                src_apks.append(fulldirfile)

This way we can hit the number of channels of the package almost as much as the time to hit a package, because the other package through this script is a copy of the file decompression and compression, almost not much time.

Third, some of the things after packing

Some time ago let me depressed is packing time long, there is a more depressing thing is to play after the package and QQ to the relevant colleagues (almost 1G of the package, very scary), so I made a little change to the above Python, the package according to the specific package name (APPID) to establish a directory, Store in the appropriate directory (then subdivide the catalog according to the packing time).

‘G:/XXX打包/‘ +  package_name +‘/output_‘‘/‘

This is good to do, and then use the FTP to share the root directory, so that the package only need to send him a vibration window can be, he can go to fetch the required package, and then I think about it, so for others feel too troublesome, I added a similar to the directory of things, the same is changed Python file.

#建立目录索引open(‘G:/XXX打包/index.html‘‘r+‘‘<a href=ftp://192.168.2.54/‘‘>‘‘</a>‘content = index_file.read()index_file.seek(00)index_file.write‘</br></br>\n‘ + content)index_file.close()

This feels more convenient, just for me now packaging is not as annoying as before, I am happy to pack now, because I just need to click on the mouse to play multiple channels of the package, to meet any needs packaging. It's also a pleasant thing for a colleague who uses the installation package, because the index I've made for him is pretty good.

You can see that this is the corresponding four AppID (that is, four applications), index.html is the index, sorted by time, generally need is the latest package.

Androidstudio Multi-appid multichannel Quick pack

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.