u8sdk--quickly batch change channel number

Source: Internet
Author: User

While the game is on the go, there are other things that we have in addition to the multi-modal channel SDK. It is often said that the CPS,CPA,CPD and other situations. These are not required to access the SDK, just some advertising means. Typically, we generate several packages identified with a logical channel number based on the official SDK's game pack. Use these logical channel numbers to indicate which advertising channel this game bag belongs to.

Usually this is the need, such as: based on the official SDK package to generate the following logical channel package: Google Channel package friend Alliance channel package Baidu Promotion channel package ... The only difference we need for these channel packages is to identify which channel they belong to instantly. So, usually we just need to add a channel ID to the game. Now, then, demand is coming. In U8sdk, how do we quickly generate such a channel package? Of course, this can be done with the current U8SDK packaging script. However, usually such a logical channel package is hundreds of times, if strictly according to unpacking, modify channel number, RePack, signature such steps, then hundreds of channel package generated down, estimated at least a few hours of time. Now the question is to be possible without unpacking the case, directly modify the original package, the inside of the channel number to change a bit. The answer is yes. The APK package is essentially a ZIP archive package, but the package is strictly signed. So, can we unzip the package and change it to a file? It is really not easy to say such a document. Because after you change a file, the package has to be re-signed. Fortunately, the method is still there, that is, "in the APK signature directory is the meta-inf/directory to create a new empty file, do not need to re-sign the apk" with this method, it is no longer necessary to re-sign the APK, the speed is quite fast. Hundreds of packs, two minutes to finish!!! So now the idea is simple and clear, we only need to use the zip in the meta-inf/directory to create a channel number as the name of the empty file, and then in the U8SDK abstraction layer of the code in the read the empty file name can be the current logical channel number. Let's look at how to implement such a function in U8SDK. First we need a configuration to configure the need for all channel numbers. Then you need a game pack (APK file) that is already plugged into the official SDK. Then you can write the script. We define a modify_channels.py script:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374757677 # -*- coding: utf-8 -*-#CreateTime:2014-10-25importsysimportfile_operateimportosimportos.pathimporttimeimportzipfiledefentry():     sourceApkFile= file_operate.getFullPath("u8source.apk")     channelsFile= file_operate.getFullPath("channels.txt")      ifnot os.path.exists(channelsFile):          file_operate.printF("The channels.txt file is not exists.")          return     f= open(channelsFile)     channelLines= f.readlines()     f.close()     channels= []     ifchannelLines !=None and len(channelLines) >0:          forline inchannelLines:               targetChannel= line.strip()               channels.append(targetChannel)     else:          file_operate.printF("There is no channel configed in channels.txt")     modify(channels, sourceApkFile)defmodify(channels, sourceApkFile):     sourceApkFile= sourceApkFile.replace(‘\\‘, ‘/‘)     ifnot os.path.exists(sourceApkFile):          file_operate.printF("The source apk file is not exists")          return      tempFolder= file_operate.getFullPath(‘temp‘)     ifnot os.path.exists(tempFolder):          os.makedirs(tempFolder)      empty_file= os.path.join(tempFolder, "temp.txt")     f= open(empty_file, ‘w‘)     f.close()     forchannel inchannels:          generateNewChannelApk(sourceApkFile, empty_file, channel)     file_operate.del_file_folder(tempFolder)defgenerateNewChannelApk(sourceApkFile, empty_file, channelID):     file_operate.printF("Now to generate channel %s", channelID)     targetFolder= file_operate.getFullPath("channels")     ifnot os.path.exists(targetFolder):          os.makedirs(targetFolder)     targetApk= os.path.join(targetFolder, "u8-"+channelID+".apk")     file_operate.copy_file(sourceApkFile, targetApk)     zipped= zipfile.ZipFile(targetApk, ‘a‘, zipfile.ZIP_DEFLATED)     emptyChannelFile= "META-INF/u8channel_{channel}".format(channel=channelID)     zipped.write(empty_file, emptyChannelFile)     zipped.close()entry()

This script has several notes: 1, the game package name must be u8source.apk. Of course you can modify into other 2, the channel number of the configuration file must be Channels.txt, and each line is configured with a channel number 3, the script will create a temporary temp directory under the temp.txt empty file, and then add this empty file to the meta-inf/directory, and named after the u8channel_{channel number} 4, after all the packages have been hit, the temp directory 5 will be deleted, all final packages will be output to the channels directory and named after the u8-{channel number}.apk and we need to U8SDK in the abstract layer of logic, To get to the logical channel number by reading the file starting with U8channel_ in the meta-inf/directory under the APK directory.
123456789101112131415161718192021 /** * 获取CPS,CPA,CPD等非SDK联运渠道的逻辑渠道号 * @return */publicint getLogicChannel (){             if(this. channel >0){            returnthis. channel;      }            String chStr = SDKTools. getLogicChannel(application, LOGIC_CHANNEL_PREFIX);       if(!TextUtils. isEmpty(chStr)){            this. channel = Integer. valueOf(chStr);      }else{            this. channel =0;      }                          returnthis. channel;}
   This allows you to complete the reading of the logical channel number. Then there is the addition of an agreement. For example, when the game starts, sends an HTTP request to the U8server, transmits the current logical channel number and so on, then, when login authentication, also adds this logical channel number. In this way can be based on these data to do analysis and statistics, according to the data to the CPS,CPA and other channels of money.
This article was written by Little black Welcome to visit Little Black's own blog: http://www.uustory.com

u8sdk--quickly batch change channel number

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.