xapk转apk

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
import zipfile
import os
import shutil
import sys
import subprocess

def unzip_file(src, dst):
with zipfile.ZipFile(src, 'r') as z:
z.extractall(dst)

def find_files(base, ext):
result = []
for root, _, files in os.walk(base):
for f in files:
if f.endswith(ext):
result.append(os.path.join(root, f))
return result

def copy_files(files, dst):
os.makedirs(dst, exist_ok=True)
for f in files:
shutil.copy(f, dst)

def copy_obb(base, dst):
for root, dirs, _ in os.walk(base):
for d in dirs:
if d == 'obb':
shutil.copytree(os.path.join(root, d), os.path.join(dst, d), dirs_exist_ok=True)

def adb_install_multiple(apk_dir):
apk_list = sorted(find_files(apk_dir, '.apk'))
if not apk_list:
print('no apk files to install')
return
print('installing via adb...')
cmd = ['adb', 'install-multiple'] + apk_list
try:
subprocess.run(cmd, check=True)
print('install success')
except subprocess.CalledProcessError:
print('install failed')

def convert_xapk(xapk_path, output_dir=None, auto_install=False):
if not os.path.exists(xapk_path):
print('file not found')
return
if output_dir is None:
output_dir = os.path.splitext(xapk_path)[0]
os.makedirs(output_dir, exist_ok=True)

unzip_file(xapk_path, output_dir)
apk_dir = os.path.join(output_dir, 'apk')
obb_dir = os.path.join(output_dir, 'obb')

apk_files = find_files(output_dir, '.apk')
if apk_files:
copy_files(apk_files, apk_dir)
print('apk extracted')

copy_obb(output_dir, obb_dir)
print('obb extracted')

if auto_install:
adb_install_multiple(apk_dir)

print('done:', output_dir)

if __name__ == '__main__':
if len(sys.argv) < 2:
print('usage: python xapk_to_apk.py app.xapk [output_dir] [--install]')
else:
xapk = sys.argv[1]
out = sys.argv[2] if len(sys.argv) > 2 and not sys.argv[2].startswith('--') else None
install_flag = '--install' in sys.argv
convert_xapk(xapk, out, install_flag)