script.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # coding:utf8
  2. __author__ = 'dong'
  3. import os
  4. import logging
  5. import xml.dom.minidom
  6. import re
  7. import shutil
  8. import glob
  9. from xml.etree.ElementTree import ElementTree, Element
  10. import distutils.dir_util
  11. from xml.etree import ElementTree as ET
  12. import gw_file_system
  13. import gw_data_center
  14. ANDROID_NS = 'http://schemas.android.com/apk/res/android'
  15. def script_init(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  16. # gw_data_center.pack_small=True
  17. return
  18. def script_second(workspace_sdk_dir, extract_dir, channel_sdk_info, new_game_channel_info, game_info):
  19. return
  20. logging.info('script_second: ' + str(channel_sdk_info))
  21. # 逆向渠道id
  22. if channel_sdk_info['id'] == '597':
  23. public_xml = os.path.join(extract_dir, "res/values/public.xml")
  24. handle_public_xml(public_xml, "attr-private")
  25. public_xml = os.path.join(extract_dir, "res/layout/design_bottom_sheet_dialog.xml")
  26. handle_public_xml(public_xml, "attr-private")
  27. public_xml = os.path.join(extract_dir, "res/layout/mtrl_layout_snackbar.xml")
  28. # handle_public_xml(public_xml, "attr-private")
  29. os.remove(public_xml)
  30. public_xml = os.path.join(extract_dir, "res/layout-sw600dp/mtrl_layout_snackbar.xml")
  31. # handle_public_xml(public_xml, "attr-private")
  32. os.remove(public_xml)
  33. public_xml = os.path.join(extract_dir, "res/values/public.xml")
  34. handle_public_xml(public_xml, "mtrl_layout_snackbar")
  35. public_xml = os.path.join(extract_dir, "res/layout/mtrl_layout_snackbar_include.xml")
  36. handle_public_xml(public_xml, "attr-private")
  37. public_xml = os.path.join(extract_dir, "res/drawable-v21/abc_dialog_material_background.xml")
  38. handle_public_xml(public_xml, "attr-private")
  39. public_xml = os.path.join(extract_dir, "res/values/styles.xml")
  40. handle_public_xml(public_xml, "attr-private")
  41. public_xml = os.path.join(extract_dir, "res/values-v21/styles.xml")
  42. handle_public_xml(public_xml, "attr-private")
  43. return
  44. def handle_public_xml(public_xml_path, remove_node):
  45. if not os.path.exists(public_xml_path):
  46. logging.info('public_xml is null: ' + public_xml_path)
  47. return
  48. new_lines = []
  49. with open(public_xml_path, 'r+') as f:
  50. for line in f.readlines():
  51. # l = line.strip()
  52. if line.find(remove_node) > -1:
  53. continue
  54. new_lines.append(line)
  55. f.seek(0)
  56. f.truncate()
  57. f.writelines(new_lines)
  58. return
  59. def script(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
  60. logging.info("-----debug-----")
  61. def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
  62. # if channelSdkInfo['id'] == '549' or channelSdkInfo['id'] == '591':
  63. # if channelSdkInfo['id'] == '597':
  64. # s1 = os.path.join(decompileDir, "smali")
  65. # if os.path.exists(s1):
  66. # smali_classes_new = create_mutil_smali(decompileDir)
  67. # s3 = os.path.join(decompileDir, smali_classes_new,)
  68. # list_package = ["androidx"]
  69. # if not os.path.exists(s3):
  70. # distutils.dir_util.mkpath(s3)
  71. # move_package(s1, s3, list_package)
  72. #
  73. # s1 = os.path.join(decompileDir, "smali_classes2","com")
  74. # if os.path.exists(s1):
  75. # smali_classes_new = create_mutil_smali(decompileDir)
  76. # s3 = os.path.join(decompileDir, smali_classes_new,"com")
  77. # list_package = ["alipay","android","asus","bumptech","bun","bytedance","cmic","coolpad","flamingo_inc","google","hg6wan","huawei","ipaynow","llxyy"]
  78. # if not os.path.exists(s3):
  79. # distutils.dir_util.mkpath(s3)
  80. # move_package(s1, s3, list_package)
  81. if channelSdkInfo['id'] == '597':
  82. s1 = os.path.join(decompileDir, "smali","androidx")
  83. if os.path.exists(s1):
  84. smali_classes_new = create_mutil_smali(decompileDir)
  85. s3 = os.path.join(decompileDir, smali_classes_new,"androidx")
  86. list_package = ["activity","appcompat","collection","core","customview","exifinterface","interpolator","lifecycle","annotation","arch","constraintlayout","cursoradapter","drawerlayout","fragment","legacy","loader"]
  87. if not os.path.exists(s3):
  88. distutils.dir_util.mkpath(s3)
  89. move_package(s1, s3, list_package)
  90. return
  91. def create_mutil_smali(decompileDir):
  92. f_idx = 2
  93. while True:
  94. tmp = gw_file_system.get_full_path(os.path.join(decompileDir, 'smali_classes%d' % f_idx))
  95. tmp = tmp.replace('\\', '/')
  96. tmp = re.sub('/+', '/', tmp)
  97. if os.path.exists(tmp):
  98. f_idx += 1
  99. else:
  100. smali_classes2_dir = tmp
  101. break
  102. # endwhile
  103. os.mkdir(smali_classes2_dir)
  104. return smali_classes2_dir
  105. def move_package(s1, s3, list_package):
  106. for d in list_package:
  107. src = os.path.join(s1, d)
  108. if os.path.exists(src):
  109. dst = os.path.join(s3, d)
  110. distutils.dir_util.copy_tree(src, dst)
  111. distutils.dir_util.remove_tree(src)
  112. def move_package_one(src, dst):
  113. if os.path.exists(src):
  114. distutils.dir_util.copy_tree(src, dst)
  115. distutils.dir_util.remove_tree(src)
  116. change_map = {".super Landroid/app/Application;": ".super Lcom/kf/framework/KFApplication;",
  117. "invoke-direct {p0}, Landroid/app/Application;-><init>()V": "invoke-direct {p0}, Lcom/kf/framework/KFApplication;-><init>()V",
  118. "invoke-super {p0, p1}, Landroid/app/Application;->attachBaseContext(Landroid/content/Context;)V": "invoke-super {p0, p1}, Lcom/kf/framework/KFApplication;->attachBaseContext(Landroid/content/Context;)V",
  119. "invoke-super {p0}, Landroid/app/Application;->onCreate()V": "invoke-super {p0}, Lcom/kf/framework/KFApplication;->onCreate()V", }
  120. def replace_super_application(decompileDir):
  121. smali_files = glob.glob(
  122. os.path.join(decompileDir, 'smali*/com/radical/huangshangjixiang/qh360/CoronaApplication.smali'))
  123. if len(smali_files) == 1:
  124. game_application_file = smali_files[0];
  125. print game_application_file
  126. if os.path.isfile(game_application_file):
  127. with open(game_application_file, "r+") as f:
  128. file_str = f.read()
  129. for k, v in change_map.items():
  130. file_str = file_str.replace(k, v)
  131. with open(game_application_file, "w+") as f:
  132. f.write(file_str)
  133. def modify_manifest(decompileDir, removeKey):
  134. ET.register_namespace('android', ANDROID_NS)
  135. xmlparse = os.path.join(decompileDir, 'AndroidManifest.xml')
  136. root_node = ET.parse(xmlparse)
  137. root = root_node.getroot()
  138. name = '{' + ANDROID_NS + '}name'
  139. authorities = '{' + ANDROID_NS + '}' + removeKey
  140. package_name = root.attrib.get('package')
  141. if package_name == None:
  142. return
  143. providers = root.findall('./application')
  144. if providers != None:
  145. for provider in providers:
  146. # providerName = provider.attrib.get(name)
  147. # if 'com.netease.ntunisdk.CcMomentRecordingForegroundService' == providerName:
  148. # 使用try 主要是为了 防止此属性不在时,导致的错误,而程序终止
  149. try:
  150. del provider.attrib[authorities]
  151. except:
  152. ""
  153. root_node.write(xmlparse, 'utf-8')
  154. if __name__ == '__main__':
  155. # replace_super_application("D:\work\wzdq")
  156. modify_manifest("E:\\apk\\youhua\\youhua.zip.out", "requestLegacyExternalStorage")