merge_share_sdk.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # -*- coding:utf-8 -*-
  2. from print_log import printlog
  3. import package_utils, path_utils, file_utils, update_channnel_sdk_v2
  4. import os
  5. from xml.etree import ElementTree as ET
  6. from xml.etree.ElementTree import SubElement
  7. androidNS = 'http://schemas.android.com/apk/res/android'
  8. scheme = '{' + androidNS + '}scheme'
  9. name_key = '{' + androidNS + '}name'
  10. def start_merge(apk_decompile_tmp_dir, config):
  11. share_sdk_path = os.path.join(path_utils.get_sdk_channel_path('share_sdk'), 'pretreatment_project')
  12. printlog('复制分享SDK资源:%s' % share_sdk_path)
  13. package_utils.remove_same_values_res(apk_decompile_tmp_dir, share_sdk_path)
  14. pre_res_dir = os.path.join(share_sdk_path, "res")
  15. update_channnel_sdk_v2.copy_res_to_apk(pre_res_dir, os.path.join(apk_decompile_tmp_dir, 'res'))
  16. # 拷贝assets
  17. printlog('copy assets...')
  18. sdk_assets_path = os.path.join(share_sdk_path, 'assets')
  19. temp_assets_path = os.path.join(apk_decompile_tmp_dir, 'assets')
  20. # 拷贝smali_classes2
  21. printlog('copy smali_classes2...')
  22. sdk_smali_path = os.path.join(share_sdk_path, 'smali_classes2')
  23. temp_smali_path = os.path.join(apk_decompile_tmp_dir, 'smali_classes2')
  24. if os.path.exists(sdk_smali_path):
  25. ret = file_utils.copy_file_all_dir(sdk_smali_path, temp_smali_path)
  26. if ret:
  27. return ret
  28. if os.path.exists(sdk_assets_path):
  29. ret = file_utils.copy_file_all_dir(sdk_assets_path, temp_assets_path)
  30. if ret:
  31. return ret
  32. # 拷贝jniLib
  33. printlog('copy lib...')
  34. temp_jni_path = os.path.join(apk_decompile_tmp_dir, 'lib')
  35. sdk_jni_path = os.path.join(share_sdk_path, 'jniLibs')
  36. if not os.path.exists(temp_jni_path):
  37. printlog('There is no need to merge lib .[temp_jni_path:]:%s' % temp_jni_path)
  38. return 0
  39. abiFilters = []
  40. if os.path.exists(temp_jni_path):
  41. for abi in os.listdir(temp_jni_path):
  42. printlog('append : ' + abi)
  43. abiFilters.append(abi)
  44. else:
  45. abiFilters = ['armeabi-v7a']
  46. if os.path.exists(sdk_jni_path):
  47. ret = file_utils.copy_file_all_dir(
  48. sdk_jni_path, temp_jni_path, False, abiFilters)
  49. if ret:
  50. return ret
  51. printlog('合并Androidmanifest.xml 文件')
  52. sdk_base_xml = os.path.join(share_sdk_path, "SDKManifest.xml")
  53. apk_decom_dir_manifest_xml = os.path.join(apk_decompile_tmp_dir, 'AndroidManifest.xml')
  54. ret = package_utils.mergeManifest(apk_decom_dir_manifest_xml, sdk_base_xml)
  55. if ret:
  56. return ret
  57. temp_yyxx_share_cfg_path = os.path.join(apk_decompile_tmp_dir, 'assets', 'yyxx_game', 'yyxx_share_cfg.properties')
  58. weibo_key = config['WEIBO_SHARE']
  59. if weibo_key:
  60. for key in weibo_key.keys():
  61. file_utils.replace_assets_param(temp_yyxx_share_cfg_path, key,
  62. weibo_key[key])
  63. qq_key = config['QQ_SHARE']
  64. if weibo_key:
  65. for key in qq_key.keys():
  66. file_utils.replace_assets_param(temp_yyxx_share_cfg_path, key,
  67. qq_key[key])
  68. if key == 'QQ_APP_KY':
  69. repalce_qq_appkey_to_manifest(apk_decompile_tmp_dir, qq_key[key])
  70. wechat_key = config['WECHAT_SHARE']
  71. if weibo_key:
  72. for key in wechat_key.keys():
  73. file_utils.replace_assets_param(temp_yyxx_share_cfg_path, key,
  74. wechat_key[key])
  75. pass
  76. def repalce_qq_appkey_to_manifest(apk_decompile_tmp_dir, appkey):
  77. manifest = os.path.join(apk_decompile_tmp_dir, 'AndroidManifest.xml')
  78. ET.register_namespace('android', androidNS)
  79. tree = ET.parse(manifest)
  80. root = tree.getroot()
  81. appNode = root.find('application')
  82. if appNode is None:
  83. return 1
  84. activityNodeList = appNode.findall('activity')
  85. if activityNodeList is None:
  86. return 1
  87. for activityNode in activityNodeList:
  88. activityName = activityNode.get(name_key)
  89. if activityName == 'com.tencent.tauth.AuthActivity':
  90. intentFilters = activityNode.findall('intent-filter')
  91. if intentFilters is not None and len(intentFilters) > 0:
  92. for intentNode in intentFilters:
  93. dataNode = intentNode.find('data')
  94. intentNode.remove(dataNode)
  95. dataNode = SubElement(intentNode, 'data')
  96. dataNode.set(scheme, 'tencent' + appkey)
  97. tree.write(manifest, 'UTF-8')