import os.path import xml.etree.ElementTree as ET import file_utils import xml_utils namespaces = {'android': 'http://schemas.android.com/apk/res/android'} encoding = 'UTF-8' def execute(game, sdk, config): if not check_config(config): return 1 sub_channel = config['subChannel'] create_qingshi_properties(game, sdk, sub_channel, config) modify_game_activity(game, sdk, sub_channel, config) def check_config(config) -> bool: """ 检查配置 """ if 'properties' not in config: print('properties not exists in config') return False properties = config['properties'] if 'QS_PACKAGE_VERSION' not in properties: print('QS_PACKAGE_VERSION not exists in properties') return False if 'QS_CAMPAIGN_ID' not in properties: print('QS_CAMPAIGN_ID not exists in properties') return False if 'QS_APP_ID' not in properties: print('QS_APP_ID not exists in properties') return False if 'QS_APP_KEY' not in properties: print('QS_APP_KEY not exists in properties') return False if 'QS_ONLINE_ENV' not in properties: print('QS_ONLINE_ENV not exists in properties') return False return True def create_qingshi_properties(game, sdk, sub_channel, config): """ 创建/assets/qs_game/qs_cfg.properties """ print('create /assets/qs_game/qs_cfg.properties') add_analytics_id(config) prop_value = config['properties'] decompile_path = file_utils.get_decompile_path(game, sdk, sub_channel, config['cache']) qs_game_path = os.path.join(decompile_path, 'assets', 'qs_game') properties = os.path.join(qs_game_path, 'qs_cfg.properties') if os.path.exists(properties): os.remove(properties) content = '' for key in prop_value: content = '%s%s=%s\n' % (content, key, prop_value[key]) print('write properties : %s = %s' % (key, prop_value[key])) file_utils.create_file(properties, content) return 0 def modify_game_activity(game, sdk, sub_channel, config): if 'packageName' not in config or 'oldPackageName' not in config: print('modify game activity action failed!!!') return package_name = config['packageName'] old_package_name = config['oldPackageName'] decompile_path = file_utils.get_decompile_path(game, sdk, sub_channel, config['cache']) manifest = os.path.join(decompile_path, 'AndroidManifest.xml') attr_name = xml_utils.get_namespaces_format('android:name', namespaces) for key in namespaces: ET.register_namespace(key, namespaces[key]) target_tree = ET.parse(manifest) target_root = target_tree.getroot() appNode = target_root.find('application') activitys = appNode.findall('activity') for activity in activitys: intent_filter = activity.find('intent-filter') if intent_filter is not None: action = intent_filter.find('action') if action is not None and action.attrib[attr_name] == old_package_name: action.attrib[attr_name] = package_name break target_tree.write(manifest, encoding) def add_analytics_id(config): if 'logSdk' not in config: return print('add analytics id...') log_sdk = config['logSdk'] if log_sdk[0] == 'jrtt': config['properties']['QS_ANALYTICS_ID'] = '1' elif log_sdk == 'gdt': pass elif log_sdk == 'ks': pass elif log_sdk == 'uc': pass elif log_sdk == 'bd': pass