sdk_script.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import file_utils
  2. import xml_utils
  3. import common_utils
  4. import os.path
  5. import xml.etree.ElementTree as ET
  6. encoding = 'UTF-8'
  7. namespaces = {'android' : 'http://schemas.android.com/apk/res/android'}
  8. def execute(game, sdk, config):
  9. if not checkConfig(config):
  10. return 1
  11. subChannel = config['subChannel']
  12. createJmhyProperties(game, sdk, subChannel, config)
  13. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  14. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  15. # xml_utils.changeLauncherAttr(manifest, 'configChanges', 'orientation|screenSize|keyboardHidden')
  16. activitys = xml_utils.getLauncherActivitys(manifest)
  17. attrName = xml_utils.getNamespacesFormat('android:name', namespaces)
  18. #修改原启动Activity
  19. for activity in activitys:
  20. activityName = activity.attrib[attrName]
  21. print ('activityName ----> ' + activityName)
  22. if activityName == 'com.lenovo.lsf.gamesdk.ui.WelcomeActivity':
  23. continue
  24. else : #游戏、渠道共2个启动类 非渠道WelcomeActivity的 则执行操作 目的:launcher属性改掉 默认启动渠道的闪屏欢迎页 跳到action为Lenovo.MAIN的游戏启动类
  25. removeLauncherActivity = changeLauncherActivityByName(manifest,activityName)
  26. print('removeLauncherAct---------> ' + removeLauncherActivity)
  27. return 0
  28. def changeLauncherActivityByName(manifest,name):
  29. for key in namespaces:
  30. ET.register_namespace(key, namespaces[key])
  31. tree = ET.parse(manifest)
  32. root = tree.getroot()
  33. attrName = xml_utils.getNamespacesFormat('android:name', namespaces)
  34. for node in root.findall('application/activity'):
  35. print('node ----> ' + node.attrib[attrName])
  36. if node.attrib[attrName] != name:
  37. continue
  38. for sub in node.getchildren():
  39. if sub.tag != 'intent-filter':
  40. continue
  41. for sub2 in sub.getchildren():
  42. if (sub2.tag == 'action' and sub2.attrib[attrName] == 'android.intent.action.MAIN'):
  43. sub2.attrib[attrName]='lenovoid.MAIN'
  44. if (sub2.tag == 'category' and sub2.attrib[attrName] == 'android.intent.category.LAUNCHER'):
  45. sub2.attrib[attrName]='android.intent.category.DEFAULT'
  46. tree.write(manifest, encoding)
  47. return node.attrib[attrName]
  48. return 0
  49. def checkConfig(config):
  50. '''
  51. 检查配置
  52. '''
  53. if 'properties' not in config:
  54. print('properties not exists in config')
  55. return False
  56. properties = config['properties']
  57. if 'agent' not in properties or 'version' not in properties:
  58. print('agent or version not exists in properties')
  59. return False
  60. '''if 'appid' not in config or 'appkey' not in config:
  61. print('appid or appkey not exists in config')
  62. return False'''
  63. return True
  64. def createJmhyProperties(game, sdk, subChannel, config):
  65. '''
  66. 创建jmhy.properties
  67. '''
  68. print('create jmhy.properties')
  69. propValue = config['properties']
  70. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  71. properties = os.path.join(decompliePath, 'assets', 'jmhy.properties')
  72. content = ''
  73. for key in propValue:
  74. content = '%s%s=%s\n' % (content, key, propValue[key])
  75. file_utils.createFile(properties, content)
  76. return 0
  77. def addMetaData(game, sdk, subChannel, config, path):
  78. '''
  79. 添加meta-data
  80. '''
  81. if 'metaData' not in config:
  82. return 0
  83. print('add meta-data...')
  84. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, path)
  85. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  86. xml_utils.addMetaData(manifest, config['metaData'])
  87. return 0
  88. def addLauncherActivity(manifest, screenOrientation, activity):
  89. '''
  90. 添加启动的activity
  91. '''
  92. for key in namespaces:
  93. ET.register_namespace(key, namespaces[key])
  94. tree = ET.parse(manifest)
  95. root = tree.getroot()
  96. # activity
  97. '''<activity android:name=".LauncherActivity"
  98. android:theme="@style/LauncherStyle">
  99. <intent-filter>
  100. <action android:name="android.intent.action.MAIN" />
  101. <category android:name="android.intent.category.LAUNCHER" />
  102. </intent-filter>
  103. </activity>'''
  104. activity = ET.Element('activity', {'android:name' : activity,
  105. 'android:theme' : '@android:style/Theme.Holo.Light.NoActionBar.Fullscreen',
  106. 'android:launchMode' : 'singleTop',
  107. 'android:configChanges' : 'orientation|screenSize|keyboardHidden',
  108. 'android:screenOrientation' : screenOrientation})
  109. intent = ET.Element('intent-filter')
  110. action = ET.Element('action', {'android:name' : 'lenovoid.MAIN'})
  111. category = ET.Element('category', {'android:name' : 'android.intent.category.DEFAULT'})
  112. intent.append(action)
  113. intent.append(category)
  114. activity.append(intent)
  115. application = root.find('application')
  116. application.insert(0, activity)
  117. tree.write(manifest, encoding)