# -*- coding:utf-8 -*- import os, json, shutil import xml.etree.ElementTree as ET import traceback import file_utils, contants, http_utils, text_utils, apk_tool, icon_utils, Replace import print_log from print_log import printlog import time def startMerge(config_json_path): global gcp_code isSuccess = False errorMsg = "切包失败,请联系开发人员" apk_decompile_tmp_dir = "" try: # 当前脚本路径 # PrintLog "[current_py_path]: %s"%sys.argv[0] # py_dir = sys.path[0] # PrintLog "[py_dir] : %s"%py_dir # 更新svn库,1脚本文件;2sdk内容;3sdk_config;4tools;5doc etc # os.system("svn up " + py_dir + DIR_SPLIT + ".." + DIR_SPLIT) # 第一个参数json文件路径 # config_json_path = sys.argv[1] # 包编号 time_start = time.time() json_text = file_utils.read_file(config_json_path) config = json.loads(json_text) gcp_code = config["gcp_code"] print_log.LOGFILE = "%s/%s.txt" % (contants.sdk_log_path, gcp_code) if os.path.exists(print_log.LOGFILE): os.remove(print_log.LOGFILE) http_utils.notify_cut_state(gcp_code, "5", "正在读取配置") printlog("[LOGFILE] : %s" % print_log.LOGFILE) printlog("[gcp_code] : %s" % gcp_code) printlog("[config_json_path] : %s" % config_json_path) # apk文件的全路径(eg: /sdk/develop/tools/outputs/mxd/package/0.01.150608/360/360_0.01.150608_high_all.apk ) origin_apk_full_path = config["apk_path"] # sys.argv[1] printlog("[origin_apk_full_path] : %s" % origin_apk_full_path) if not os.path.exists(origin_apk_full_path): errorMsg = "找不到游戏母包,请检查" return isSuccess # apk文件全名(eg: 360_0.01.150608_high_all.apk ) origin_apk_full_name = os.path.basename(origin_apk_full_path) printlog("[origin_apk_full_name] : %s" % origin_apk_full_name) # apk文件名(eg: 360_0.01.150608_high_all) origin_apk_name = os.path.splitext(origin_apk_full_name)[0] printlog("[origin_apk_name] : %s" % origin_apk_name) # apk文件路径(eg: /sdk/develop/tools/outputs/mxd/package/0.01.150608/360 ) origin_apk_dir = os.path.dirname(origin_apk_full_path) printlog("[origin_apk_dir] : %s" % origin_apk_dir) # 渠道名 sdk_name = config["channel_key"] # sys.argv[2] printlog("[sdk_name] : %s" % sdk_name) # 包名 package_name = config["package_name"] # sys.argv[3] printlog("[package_name] : %s" % package_name) result = text_utils.isMatchRegExp(package_name, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.") if not result: errorMsg = "包名包含特殊字符,不允许切包" return isSuccess # 游戏编号 gid = config['gid'] printlog("[gid] : %s" % gid) # 游戏c_id cid = config['cid'] printlog("[cid] : %s" % cid) # 渠道文件的根目录 sdk_root_dir = contants.channel_sdk_path printlog("[sdk_root_dir] : %s" % sdk_root_dir) # 打包渠道SDK的目录 channel_sdk_root_dir = os.path.join(sdk_root_dir, sdk_name) printlog("[channel_sdk_root_dir] : %s" % channel_sdk_root_dir) if not os.path.exists(channel_sdk_root_dir): errorMsg = "该渠道尚未接入完成" return isSuccess # keystore配置文件 keystore_path = config['keystore_path'] printlog("[keystore_path] : %s" % keystore_path) if not os.path.exists(keystore_path): errorMsg = "找不到签名文件,请检查" return isSuccess storepass = config['storepass'] printlog("[storepass] : %s" % storepass) keypass = config['keypass'] printlog("[keypass] : %s" % keypass) alias = config['alias'] printlog("[alias] : %s" % alias) # 反编译原apk文件的输出路径 apk_decompile_out_dir = os.path.join(origin_apk_dir, origin_apk_name) printlog("[apk_decompile_out_dir] : %s" % apk_decompile_out_dir) # icon路径 icon_path = config['icon'] printlog("[icon_path]: %s" % icon_path) meta_config = config['meta_config'] # 版本号 version = meta_config['version'] printlog("[version]: %s" % version) v_code = meta_config['v_code'] printlog("[v_code]: %s" % v_code) targetSdkVersion = meta_config['targetSdkVersion'] printlog("[targetSdkVersion]: %s" % targetSdkVersion) orientation = meta_config['SDK_ORIENTATION'] printlog("[orientation]: %s" % orientation) http_utils.notify_cut_state(gcp_code, "10", "正在反编译文件") # 判断反编译出的文件修改时间与apk修改时间比较,若比apk最则重新反编译 ret = file_utils.compareFileModifyTime(origin_apk_full_path, apk_decompile_out_dir) if ret is not None: printlog("母包未更新,无需重新反编译") else: apk_tool.decompile(origin_apk_full_path, apk_decompile_out_dir) http_utils.notify_cut_state(gcp_code, "20", "正在复制文件") randomNumber = text_utils.getRamdomNumber(6) apk_decompile_tmp_dir = os.path.join(origin_apk_dir, randomNumber, 'dcm_tmp', sdk_name) printlog("[apk_decompile_tmp_dir] : %s" % apk_decompile_tmp_dir) shutil.copytree(apk_decompile_out_dir, apk_decompile_tmp_dir) http_utils.notify_cut_state(gcp_code, "30", "正在合并资源") # compileResources(apk_decompile_tmp_dir, tmp_dir, package_name) file_utils.modifyPkgName(apk_decompile_tmp_dir, package_name) # 添加打点SDK printlog("-----开始添加打点SDK -----") Replace.add_point_smali(os.path.join(sdk_root_dir, 'data-point', 'data-point'), apk_decompile_tmp_dir) file_utils.replace_assets_param( os.path.join(apk_decompile_tmp_dir, 'assets', 'datapoint_config.properties'), 'DP_BIZ_GCP_CODE', gcp_code) temp_manifest_path = os.path.join(apk_decompile_tmp_dir, 'AndroidManifest.xml') metadatas = ET.parse(temp_manifest_path).getroot().find("application").findall("meta-data") for meta in metadatas: if meta.attrib["{http://schemas.android.com/apk/res/android}name"] == "yyrh_game_code": values = meta.attrib["{http://schemas.android.com/apk/res/android}value"] file_utils.replace_assets_param(os.path.join(apk_decompile_tmp_dir, 'assets', 'datapoint_config.properties'), 'DP_BIZ_GAME_CODE', values) printlog("-----打点SDK添加完成 -----") # 合并Manifest文件 file_utils.mergeManifest(os.path.join(sdk_root_dir, sdk_name, sdk_name + '_sdk_config.xml'), temp_manifest_path) # 合并资源 Replace.mergeResources(os.path.join(sdk_root_dir, sdk_name, sdk_name), apk_decompile_tmp_dir) # 生成R文件 # Aapt_Util.createRFile(apk_decompile_tmp_dir,True, package_name) temp_yyrh_cnf_path = os.path.join(apk_decompile_tmp_dir, 'assets', 'YyrhParam.cnf') # 替换YyrhParam中的gcp_code printlog(temp_yyrh_cnf_path) file_utils.replace_assets_param(temp_yyrh_cnf_path, "GCP_CODE", gcp_code) # 替换Androidmanifest.xml中的yyrh_gameChannelID为gcp_code yyrh_game_channel_id file_utils.replace_manifest_meta_data(temp_manifest_path, "yyrh_gameChannelID", gcp_code) # 修改debuggable为false # Replace.replaceAM_Debuggable("%s/AndroidManifest.xml"%apk_decompile_out_dir) # 替换Icon http_utils.notify_cut_state(gcp_code, "40", "正在修改资源") # 替换Icon printlog("----- begin replace resource -----") if icon_path: icon_utils.replace_icon(apk_decompile_tmp_dir, icon_path) # 替换启动页 splash_path = config['splash'] if splash_path: if os.path.exists(splash_path): shutil.copy(splash_path, "%s/assets/yyrh_start_image.jpg" % apk_decompile_tmp_dir) else: errorMsg = "找不到闪屏文件,请检查" return isSuccess ########### #sdk 关键字 http_utils.notify_cut_state(gcp_code, "50", "正在替换渠道参数") # 替换Androidmanifest.xml中的 package_name值为包名 file_utils.replace_content(temp_manifest_path, 'package_name', package_name) sdk_client_config = config['sdk_client_config'] printlog('[sdk_client_config] : %s' % sdk_client_config) if sdk_client_config: keys = sdk_client_config.keys() for key in keys: # 替换AndroidManifest.xml中的meta-data字段 file_utils.replace_manifest_meta_data(temp_manifest_path, key, sdk_client_config[key]) # 替换assets/YyrhParam.cnf中的关键字 file_utils.replace_assets_param(temp_yyrh_cnf_path, key, sdk_client_config[key]) # 特殊渠道需要特殊处理 Replace.special_replace(sdk_name, apk_decompile_tmp_dir, key, sdk_client_config[key]) ########## #game 关键字 printlog("meta_config:%s" % meta_config) if meta_config: keys = meta_config.keys() for key in keys: if key == "APP_NAME": file_utils.replace_string_app_name(apk_decompile_tmp_dir, meta_config[key]) elif key == "version": version = meta_config[key] elif key == "v_code": v_code = meta_config[key] elif key == "targetSdkVersion": targetSdkVersion = meta_config[key] # 替换方向 elif key == "SDK_ORIENTATION": file_utils.replace_content(temp_manifest_path, 'sdk_orientation', meta_config[key]) file_utils.replace_assets_param(temp_yyrh_cnf_path, "SDK_ORIENTATION", meta_config[key]) # 修改版本号 http_utils.notify_cut_state(gcp_code, "60", "正在修改版本号") yml = os.path.join(apk_decompile_tmp_dir, 'apktool.yml') file_utils.changeVersion(yml, v_code, version, targetSdkVersion) file_utils.change_min_sdk_version(yml, 21) file_utils.merge_wx_page(package_name, apk_decompile_tmp_dir) http_utils.notify_cut_state(gcp_code, "70", "正在替换游戏资源") game_resource_replace = config['game_resource_replace'] printlog('[game_resource_replace]:%s' % game_resource_replace) if game_resource_replace: for resource in game_resource_replace: if os.path.exists(resource[1]): file_utils.copy_file(resource[1], os.path.join(apk_decompile_tmp_dir, resource[0]), False) out_put_unsigned_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_unsigned.apk') out_put_signed_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_signed.apk') out_put_zipalign_apk_path = os.path.join(apk_decompile_tmp_dir, 'gen', '_zipaligned.apk') # 再编译 http_utils.notify_cut_state(gcp_code, "80", "正在回编译APK") if apk_tool.recompile(apk_decompile_tmp_dir, out_put_unsigned_apk_path): errorMsg = 'apk回编译失败' return isSuccess http_utils.notify_cut_state(gcp_code, "90", "开始重签名,对齐APK") if apk_tool.V1signer(out_put_unsigned_apk_path, out_put_signed_apk_path, keystore_path, storepass, alias, keypass): errorMsg = 'apk回编译失败' return isSuccess printlog("开始执行apk压缩") if apk_tool.zipalign(out_put_signed_apk_path, out_put_zipalign_apk_path): errorMsg = 'apk压缩失败' return isSuccess dis_path = os.path.join(contants.out_put_dir, "g%s_%s_%s_%s.apk" % (gid, sdk_name, package_name, gcp_code)) apkDstDir = os.path.dirname(dis_path) if not os.path.exists(apkDstDir): os.makedirs(apkDstDir) shutil.copyfile(out_put_zipalign_apk_path, dis_path) time_end = time.time() time_c = time_end - time_start printlog("APK路径:%s" % dis_path) printlog("切包总用时:%s秒" % time_c) isSuccess = True except Exception as err: printlog(errorMsg) printlog("cut error occur:%s" % err) printlog(traceback.format_exc()) finally: if isSuccess: http_utils.notify_cut_state(gcp_code, "100", "切包成功(点击复制链接)") else: http_utils.notify_cut_state(gcp_code, "99", errorMsg) if apk_decompile_tmp_dir: printlog('清空打包临时目录') printlog('[apk_decompile_tmp_dir]:%s' % os.path.dirname(apk_decompile_tmp_dir)) file_utils.delete_folder(os.path.dirname(apk_decompile_tmp_dir)) return isSuccess if __name__ == "__main__": pass