sdk_script.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import file_utils
  2. import common_utils
  3. import xml_utils
  4. import os.path
  5. import xml.etree.ElementTree as ET
  6. namespaces = {'android' : 'http://schemas.android.com/apk/res/android'}
  7. encoding = 'UTF-8'
  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. if config['addLauncher']:
  14. addLauncher(game, sdk, subChannel, config)
  15. common_utils.changeApplication(game, sdk, subChannel, config, 'com.beiyu.anycommon.AnyApplication')
  16. return 0
  17. def checkConfig(config):
  18. '''
  19. 检查配置
  20. '''
  21. if 'properties' not in config:
  22. print('properties not exists in config')
  23. return False
  24. properties = config['properties']
  25. if 'agent' not in properties or 'version' not in properties:
  26. print('agent or version not exists in properties')
  27. return False
  28. '''if 'appid' not in config or 'appkey' not in config:
  29. print('appid or appkey not exists in config')
  30. return False'''
  31. return True
  32. def createJmhyProperties(game, sdk, subChannel, config):
  33. '''
  34. 创建jmhy.properties
  35. '''
  36. print('create jmhy.properties')
  37. propValue = config['properties']
  38. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  39. properties = os.path.join(decompliePath, 'assets', 'jmhy.properties')
  40. content = ''
  41. for key in propValue:
  42. content = '%s%s=%s\n' % (content, key, propValue[key])
  43. file_utils.createFile(properties, content)
  44. return 0
  45. def addLauncher(game, sdk, subChannel, config):
  46. '''
  47. 添加启动图
  48. '''
  49. decompliePath = file_utils.getDecompliePath(game, sdk, subChannel, config['cache'])
  50. # 修改主文件信息
  51. print('change launcher config...')
  52. manifest = os.path.join(decompliePath, 'AndroidManifest.xml')
  53. activity = xml_utils.getLauncherActivityName(manifest)
  54. if activity == 'com.beiyu.anycore.SplashScreenActivity':
  55. print('add launcher already exist...')
  56. return 1
  57. activity = xml_utils.removeLauncherActivity(manifest)
  58. addLauncherActivity(manifest, activity)
  59. print('change launcher %s to %s' % (activity, 'com.beiyu.anycore.SplashScreenActivity'))
  60. #config['oldLauncher'] = activity
  61. return 0
  62. def addLauncherActivity(manifest, oldActivity):
  63. '''
  64. 添加启动的activity
  65. '''
  66. for key in namespaces:
  67. ET.register_namespace(key, namespaces[key])
  68. tree = ET.parse(manifest)
  69. root = tree.getroot()
  70. # activity
  71. activity = ET.Element('activity', {'android:name' : 'com.beiyu.anycore.SplashScreenActivity',
  72. 'android:configChanges' : 'fontScale|orientation|keyboardHidden|locale|navigation|screenSize|uiMode',
  73. 'android:noHistory' : 'true',
  74. 'android:stateNotNeeded' : 'true',
  75. 'android:theme' : '@android:style/Theme.NoTitleBar.Fullscreen',
  76. 'android:screenOrientation' : 'portrait'})
  77. intent = ET.Element('intent-filter')
  78. action = ET.Element('action', {'android:name' : 'android.intent.action.MAIN'})
  79. category = ET.Element('category', {'android:name' : 'android.intent.category.LAUNCHER'})
  80. metaData = ET.Element('meta-data', {'android:name' : 'com.beiyu.intent.main', 'android:value' : oldActivity})
  81. intent.append(action)
  82. intent.append(category)
  83. activity.append(metaData)
  84. activity.append(intent)
  85. application = root.find('application')
  86. application.insert(0, activity)
  87. tree.write(manifest, encoding)