game_utils.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import os.path
  2. import xml.etree.ElementTree as ET
  3. import file_utils
  4. import xml_utils
  5. namespaces = {'android': 'http://schemas.android.com/apk/res/android'}
  6. encoding = 'UTF-8'
  7. def handle_lebian_sdk_manifest(game, sdk, config):
  8. """
  9. 乐变的处理,理论上只需要有接入的游戏需要处理
  10. 考虑到技术人员不可控,所以每个游戏都处理
  11. """
  12. print('change Provider Authorities')
  13. subChannel = config['subChannel']
  14. decompliePath = file_utils.get_decompile_path(game, sdk, subChannel, config['cache'])
  15. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  16. change_provider_authorities(manifest, config['packageName'])
  17. return 0
  18. def change_provider_authorities(manifest, package_name):
  19. """
  20. 标识为com.excelliance
  21. 更改provider的android:authorities
  22. 更改activity的android:taskAffinity
  23. """
  24. for key in namespaces:
  25. ET.register_namespace(key, namespaces[key])
  26. tree = ET.parse(manifest)
  27. root = tree.getroot()
  28. packageTag = 'com.excelliance'
  29. attrAuthorities = xml_utils.get_namespaces_format('android:authorities', namespaces)
  30. for provider in root.findall('application/provider'):
  31. if attrAuthorities not in provider.attrib:
  32. continue
  33. authorities = provider.attrib[attrAuthorities]
  34. if packageTag not in authorities:
  35. continue
  36. auth = package_name + authorities[authorities.index(':'):]
  37. provider.attrib[attrAuthorities] = auth
  38. attrName = xml_utils.get_namespaces_format('android:name', namespaces)
  39. attrTaskAffinity = xml_utils.get_namespaces_format('android:taskAffinity', namespaces)
  40. for activity in root.findall('application/activity'):
  41. if packageTag not in activity.attrib[attrName]:
  42. continue
  43. if attrTaskAffinity in activity.attrib:
  44. activity.attrib[attrTaskAffinity] = package_name
  45. tree.write(manifest, encoding)