Skip to content

Commit 340a115

Browse files
committed
🐛 refactor(android): 优化 APK 工具的安全性和跨平台兼容性
1 parent 54a23c6 commit 340a115

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

android/apktools.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ def is_windows():
6666
"""Check if it's a Windows system"""
6767
return platform.system() == 'Windows'
6868

69+
def get_apksigner_shell():
70+
"""Get apksigner shell command"""
71+
return "apksigner.bat" if is_windows() else "apksigner"
6972

7073
def split_filename(abs_path):
7174
"""Extract filename from absolute path and separate basename and extension"""
@@ -495,7 +498,7 @@ def _zipalign_apk(self, apk_filename):
495498
if os.path.exists(built_apk_aligned_file):
496499
os.remove(built_apk_aligned_file)
497500

498-
run_command(['zipalign', '-p', '-f', '4', built_apk_file, built_apk_aligned_file], shell=True)
501+
run_command(['zipalign', '-p', '-f', '4', built_apk_file, built_apk_aligned_file])
499502
os.remove(built_apk_file)
500503
shutil.move(str(built_apk_aligned_file), str(built_apk_file))
501504
logger.info('Zipalign operation completed')
@@ -513,7 +516,7 @@ def _generate_keystore(self, sign_config):
513516
'-storepass', sign_config["sign.keystore.password"],
514517
'-keypass', sign_config["sign.key.password"],
515518
'-dname', f"CN={sign_config['sign.key.dname.cn']},C={sign_config['sign.key.dname.c']}"
516-
], shell=True, log=False)
519+
], log=False)
517520
logger.info('Keystore generation completed')
518521

519522
def _sign_apk(self, apk_filename):
@@ -528,20 +531,20 @@ def _sign_apk(self, apk_filename):
528531
os.remove(build_apk_signed_file)
529532

530533
run_command([
531-
'apksigner', 'sign',
534+
get_apksigner_shell(), 'sign',
532535
'--ks', os.path.join(self.keystore_dir, self.sign_properties["sign.keystore"]),
533536
'--ks-pass', f"pass:{self.sign_properties['sign.keystore.password']}",
534537
'--ks-key-alias', self.sign_properties["sign.key.alias"],
535538
'--key-pass', f"pass:{self.sign_properties['sign.key.password']}",
536539
'--out', build_apk_signed_file,
537540
build_apk_file
538-
], shell=True, log=False)
541+
], log=False)
539542

540543
os.remove(build_apk_file)
541544
shutil.move(str(build_apk_signed_file), str(build_apk_file))
542545
logger.info('APK signing completed')
543546
logger.info('Verifying APK signature')
544-
run_command(['apksigner', 'verify', '--verbose', build_apk_file], shell=True)
547+
run_command([get_apksigner_shell(), 'verify', '--verbose', build_apk_file])
545548
logger.info('APK signature verification completed')
546549

547550
def _apkm_to_apk(self, apkm_file, apk_file):
@@ -551,7 +554,7 @@ def _apkm_to_apk(self, apkm_file, apk_file):
551554
raise Exception(f"{apk_editor_jar} not found.")
552555
if os.path.exists(apk_file):
553556
os.remove(apk_file)
554-
run_command(['java', '-jar', apk_editor_jar, 'm', '-i', apkm_file, '-o', apk_file], shell=True)
557+
run_command(['java', '-jar', apk_editor_jar, 'm', '-i', apkm_file, '-o', apk_file])
555558

556559
def _decode_apk(self, apk_file, out_dir):
557560
"""Decompile APK file"""
@@ -560,7 +563,7 @@ def _decode_apk(self, apk_file, out_dir):
560563
raise Exception(f"{apk_editor_jar} not found.")
561564
if os.path.exists(out_dir):
562565
safe_rmtree(out_dir)
563-
run_command(['java', '-jar', apk_editor_jar, 'd', '-i', apk_file, '-o', out_dir], shell=True)
566+
run_command(['java', '-jar', apk_editor_jar, 'd', '-i', apk_file, '-o', out_dir])
564567

565568
def _replace_language_xml(self, target_dir):
566569
"""Replace language resource file"""
@@ -581,7 +584,7 @@ def _build_apk(self, out_dir, apk_filename):
581584
apk_file = os.path.join(self.tmp_dir, apk_filename + EXT_APK)
582585
if os.path.exists(apk_file):
583586
os.remove(apk_file)
584-
run_command(['java', '-jar', apk_editor_jar, 'b', '-i', out_dir, '-o', apk_file], shell=True)
587+
run_command(['java', '-jar', apk_editor_jar, 'b', '-i', out_dir, '-o', apk_file])
585588

586589
def _export_apk(self, apk_filename, export_filename):
587590
"""Export final APK file"""

0 commit comments

Comments
 (0)