sdk_script.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import file_utils
  2. import common_utils
  3. import xml_utils
  4. import os.path
  5. import xml.etree.ElementTree as ET
  6. import zipfile
  7. namespaces = {'android' : 'http://schemas.android.com/apk/res/android'}
  8. encoding = 'UTF-8'
  9. def execute(game, sdk, config):
  10. if not checkConfig(config):
  11. return 1
  12. subChannel = config['subChannel']
  13. createJmhyProperties(game, sdk, subChannel, config)
  14. common_utils.changeApplication(game, sdk, subChannel, config, 'com.jmhy.sdk.common.JMXmyApplication')
  15. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  16. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  17. activitys = xml_utils.getLauncherActivitys(manifest)
  18. attrName = xml_utils.getNamespacesFormat('android:name', namespaces)
  19. #修改原启动Activity
  20. for activity in activitys:
  21. activityName = activity.attrib[attrName]
  22. #('activityName ----> ' + activityName)
  23. if activityName == 'fusion.mj.communal.element.SplashScreenActivity':
  24. continue
  25. removeLauncherActivity = xml_utils.removeLauncherActivityByName(manifest,activityName)
  26. #print('222----> ' + removeLauncherActivity)
  27. #添加meta-data
  28. jsonConfig = {}
  29. meta = {}
  30. meta['fusion_entryactivity'] = removeLauncherActivity
  31. jsonConfig['metaData'] = meta
  32. addMetaData(game, sdk, subChannel, jsonConfig, config['cache'])
  33. addLauncherActivity(manifest, config['screenOrientation'], 'fusion.mj.communal.element.SplashScreenActivity')
  34. #copySmaliCode(game, sdk, subChannel, config)
  35. return 0
  36. def checkConfig(config):
  37. '''
  38. 检查配置
  39. '''
  40. if 'properties' not in config:
  41. print('properties not exists in config')
  42. return False
  43. properties = config['properties']
  44. if 'agent' not in properties or 'version' not in properties:
  45. print('agent or version not exists in properties')
  46. return False
  47. return True
  48. def createJmhyProperties(game, sdk, subChannel, config):
  49. '''
  50. 创建jmhy.properties
  51. '''
  52. print('create jmhy.properties')
  53. propValue = config['properties']
  54. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  55. properties = os.path.join(decompliePath, 'assets', 'jmhy.properties')
  56. content = ''
  57. for key in propValue:
  58. content = '%s%s=%s\n' % (content, key, propValue[key])
  59. file_utils.createFile(properties, content)
  60. return 0
  61. def addMetaData(game, sdk, subChannel, config, path):
  62. '''
  63. 添加meta-data
  64. '''
  65. if 'metaData' not in config:
  66. return 0
  67. print('add meta-data...')
  68. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, path)
  69. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  70. xml_utils.addMetaData(manifest, config['metaData'])
  71. return 0
  72. def copySmaliCode(game, sdk, subChannel, config):
  73. '''
  74. 拷贝代码
  75. '''
  76. print('copy xingmuyou smali')
  77. sdkPath = file_utils.getFullSDKPath(sdk)
  78. xmyFile = os.path.join(sdkPath, 'smali')
  79. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  80. smaliPath = os.path.join(decompliePath, 'smali')
  81. ret = file_utils.copyDir(xmyFile, smaliPath)
  82. return ret
  83. def addLauncherActivity(manifest, screenOrientation, activity):
  84. '''
  85. 添加启动的activity
  86. '''
  87. for key in namespaces:
  88. ET.register_namespace(key, namespaces[key])
  89. tree = ET.parse(manifest)
  90. root = tree.getroot()
  91. # activity
  92. '''<activity android:name=".LauncherActivity"
  93. android:theme="@style/LauncherStyle">
  94. <intent-filter>
  95. <action android:name="android.intent.action.MAIN" />
  96. <category android:name="android.intent.category.LAUNCHER" />
  97. </intent-filter>
  98. </activity>'''
  99. activity = ET.Element('activity', {'android:name' : activity,
  100. 'android:theme' : '@android:style/Theme.Holo.Light.NoActionBar.Fullscreen',
  101. 'android:launchMode' : 'singleTop',
  102. 'android:configChanges' : 'orientation|screenSize|keyboardHidden',
  103. 'android:screenOrientation' : screenOrientation})
  104. intent = ET.Element('intent-filter')
  105. action = ET.Element('action', {'android:name' : 'android.intent.action.MAIN'})
  106. category = ET.Element('category', {'android:name' : 'android.intent.category.LAUNCHER'})
  107. intent.append(action)
  108. intent.append(category)
  109. activity.append(intent)
  110. application = root.find('application')
  111. application.insert(0, activity)
  112. tree.write(manifest, encoding)