批量打包

批量打包

  • 首先修改manifest文件内容:

原文件:

package com.zcdog.util.info.android;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.text.TextUtils;

import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ManifestUtil {
    private static final String SP_CONFIG_NAME = "channel-sp";
    private static final String SP_KEY = "channel_id";
    public static final String START_FLAG = "META-INF/channel_";

    /**
     * 获取META-INFO下面的渠道
     *
     * @param context
     * @return
     */
    public static String getChannel(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SP_CONFIG_NAME, Context.MODE_PRIVATE);
        String channel = sp.getString(SP_KEY, null);
        if (!TextUtils.isEmpty(channel)) {
            return channel;
        }
        ApplicationInfo appinfo = context.getApplicationInfo();
        String sourceDir = appinfo.sourceDir;
        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.contains(START_FLAG)) {
                    channel = entryName.replaceAll(START_FLAG, "");
                    sp.edit().putString(SP_KEY, channel).apply();
                    return channel;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipfile != null) {
                try {
                    zipfile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";

    }
}

修改后的文件:

package com.zcdog.util.info.android;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ManifestUtil {

    public static final String START_FLAG = "META-INF/channel_";
    public static final String SEPARATOR = "%";
    private static final String TAG ="ManifestUtilTag";
    public static String channel;
    public static String inviteCode="";

    /**
     * 获取META-INFO下面的渠道,如果为空,尝试获取metaData下的配置的渠道
     * @param context
     * @return
     */
    public static String getChannel(Context context) {
        if (!TextUtils.isEmpty(channel)) {
            return channel;
        }
        initMetaInfData(context);
        Logger.d(TAG,"channel=="+channel);
        return channel;
    }

    /**
     * 获取邀请码
     *
     * @param context
     * @return
     */
    public static String getInviteCode(Context context) {
        if (!TextUtils.isEmpty(inviteCode)) {
            return inviteCode;
        }
        initMetaInfData(context);
        Logger.d(TAG,"inviteCode=="+inviteCode);
        return inviteCode;
    }

    /**
     * 获取Manifest里面配置的metaData数据信息
     */
    public static String getMetaDataValue(Context context, String metaDataName) {
        String channel = "";
        try {
            channel = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA).metaData.getString(metaDataName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            channel = "UnKnown_zcdog";
        }
        return channel;
    }

    /**
     * 第一次获取META-INF/channel_的数据信息
     * @param context
     */
    private static void initMetaInfData(Context context){
        ApplicationInfo appinfo = context.getApplicationInfo();
        String sourceDir = appinfo.sourceDir;
        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.contains(START_FLAG)) {
                    String channelAndInviteCode = entryName.replaceAll(START_FLAG, "");
                    //判断是否有邀请码
                    String[] valuesArray = channelAndInviteCode.split(SEPARATOR);
                    if (valuesArray != null && valuesArray.length > 1) {
                        channel = valuesArray[0];
                        inviteCode = valuesArray[1];
                    } else {
                        channel = channelAndInviteCode;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (zipfile != null) {
                try {
                    zipfile.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if (TextUtils.isEmpty(channel)) {
            channel = getMetaDataValue(context, "DISTRIBUTION_CHANNEL");
        }
    }
}
  • 然后修改Python脚本:

原脚本:

import sys,os,shutil,zipfile,time
apkVersion="3.5"
apkPrefixName="Zcdog"
srcFileName=apkPrefixName+apkVersion+"_normal.apk"
apksDir="apks"
destDir=os.path.abspath('.')
target_file="channel.apk"

file=open("channels.txt")

def writeChannelToApk(filename,channel):
        z=zipfile.ZipFile(filename,'a',zipfile.ZIP_DEFLATED)
        empty_channel_file="META-INF/channel_{channe}".format(channe=channel)
        if not os.path.exists(target_file):
            open(target_file,'a').close()
        z.write(target_file,empty_channel_file)
        z.close()
        print "writeChannelToApkchannel"+channel+","+filename+"\n"

def cpFile(srcPath,fileName):
    destPath = destDir + os.path.sep + fileName
    if os.path.exists(srcPath) and not os.path.exists(destPath):
        shutil.copy(srcPath,destPath)



if not os.path.exists(srcFileName):
    print "source file "+srcFileName+" not exists"
    sys.exit(1)


start = time.clock()

if not os.path.exists(apksDir):
    os.makedirs(apksDir)

for line in file:
    channel=line.strip('\n').strip()
    targetFileName=apksDir+"/"+apkPrefixName+apkVersion+"_"+channel+".apk"
    print "copyfile:"+targetFileName
    cpFile(srcFileName,targetFileName)
    writeChannelToApk(targetFileName,channel)
end = time.clock()

print("The function run time is : %.03f seconds" %(end-start))


#heliao-app-91-5.1.1

修改后的脚本:

import sys,os,shutil,zipfile,time
apkVersion="3.5"
apkPrefixName="Zcdog"
srcFileName=apkPrefixName+apkVersion+"_normal.apk"
apksDir="apks"
destDir=os.path.abspath('.')
target_file="channel.apk"
separator="%";
file=open("channels.txt")

def writeChannelToApk(filename,channel):
        z=zipfile.ZipFile(filename,'a',zipfile.ZIP_DEFLATED)
        empty_channel_file="META-INF/channel_{channe}".format(channe=channel)
        if not os.path.exists(target_file):
            open(target_file,'a').close()
        z.write(target_file,empty_channel_file)
        z.close()
        print "writeChannelToApkchannel"+channel+","+filename+"\n"

def cpFile(srcPath,fileName):
    destPath = destDir + os.path.sep + fileName
    if os.path.exists(srcPath) and not os.path.exists(destPath):
        shutil.copy(srcPath,destPath)



if not os.path.exists(srcFileName):
    sys.exit(1)


start = time.clock()

if os.path.exists(apksDir):
    shutil.rmtree(apksDir)
    os.makedirs(apksDir)
else:
    os.makedirs(apksDir)


for line in file:
    channelAndInviteCode=line.strip('\n').strip()
    channel=channelAndInviteCode.split(separator)[0]
    print "channel_name=="+channel
    targetFileName=apksDir+"/"+apkPrefixName+apkVersion+"_"+channel+".apk"
    print "copyfile:"+targetFileName
    cpFile(srcFileName,targetFileName)
    writeChannelToApk(targetFileName,channelAndInviteCode)
end = time.clock()

print("The function run time is : %.03f seconds" %(end-start))


#heliao-app-91-5.1.1

修改之前读取邀请码的函数:

//读取配置文件的当前渠道对应的邀请码
    AssetManager assetManager= BaseApplication.getContext().getAssets();
    InputStream ins=null;
    try {
        ins=assetManager.open("channel.properties");
        Properties properties=new Properties();
        properties.load(ins);
        String chanelName= Misc.getChannelValue();
        String inviteCode=properties.getProperty(chanelName);
        Logger.d(TAG, "inviteCode" + inviteCode + ":chanelName==" + chanelName);
        if(inviteCode!=null&&!inviteCode.isEmpty()){
            et_invite_code.setText(inviteCode);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
       if (ins!=null){
           try {
               ins.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
    }

修改之后获取邀请码的函数:

//读取配置文件的当前渠道对应的邀请码
   String inviteCode = ManifestUtil.getInviteCode(BaseApplication.getContext());
   if (!TextUtils.isEmpty(inviteCode)) {
       et_invite_code.setText(inviteCode);
   }