channel_action.py
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import os.path
from xml.etree import ElementTree as ET
from xml.etree.ElementTree import SubElement
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import ElementTree
import os
import os.path
import zipfile
import re
import subprocess
import platform
from xml.dom import minidom
import codecs
import sys
androidNS = 'http://schemas.android.com/apk/res/android'
def execute(channel, decompileDir, packageName):
replace_applicationId_to_AndroidManifest(decompileDir,packageName)
manifest_path = decompileDir + '/AndroidManifest.xml'
find_launch_activity(manifest_path)
return 0
def replace_applicationId_to_AndroidManifest(decompileDir, packageName):
manifestPath = decompileDir + '/AndroidManifest.xml'
manifest_file = open(manifestPath, 'r+', encoding='Utf-8')
manifestContent = str(manifest_file.read())
manifest_file.close()
# 4、replace "${applicationId}" "${JPUSH_PKGNAME}" with packname in AndroidManifest.xml
manifestContent = manifestContent.replace('${applicationId}', packageName)
manifest_new_file = open(manifestPath, 'w', encoding='Utf-8')
manifest_new_file.write(manifestContent)
manifest_new_file.close()
return 0
def find_launch_activity(manifest_path):
ET.register_namespace("android", androidNS)
tree = ET.parse(manifest_path)
root = tree.getroot()
for activity in root.findall('.//activity'):
intent_filter = activity.find('.//intent-filter')
if intent_filter is not None:
action = intent_filter.find('.//action')
if action is not None and action.get('{' + androidNS + '}name') == 'android.intent.action.MAIN':
origin_activity_name = activity.get('{' + androidNS + '}name')
print(origin_activity_name)
activity.remove(intent_filter)
tree.write(manifest_path)
manifest_add(manifest_path, tree, root, origin_activity_name)
break
def manifest_add(manifest_path, tree, root, origin_activity_name):
namespace2 = {'android': 'http://schemas.android.com/apk/res/android'}
target_activity = root.find(".//activity[@android:name='com.stss.sdk.SplashActivity']",
namespace2)
if target_activity is None:
return "SplashActivity not found"
intent_filter = target_activity.find('intent-filter')
if intent_filter is None:
intent_filter = ET.SubElement(target_activity, 'intent-filter')
action_element = ET.SubElement(intent_filter, 'action')
action_element.set('{' + androidNS + '}name', 'android.intent.action.MAIN')
category_element = ET.SubElement(intent_filter, 'category')
category_element.set('{' + androidNS + '}name', 'android.intent.category.LAUNCHER')
meta_data = ET.SubElement(target_activity, 'meta-data')
meta_data.set('android:name', "GAME_ACTIVITY")
meta_data.set('android:value', origin_activity_name)
tree.write(manifest_path)