imetting_backend/app/utils/apk_parser.py

37 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""
APK解析工具
用于从APK文件中提取版本信息
"""
import zipfile
import xml.etree.ElementTree as ET
import struct
# 如果安装了 androguard使用更可靠的解析方法
def parse_apk_with_androguard(apk_path):
"""
使用 androguard 库解析 APK
需要先安装: pip install androguard
"""
try:
from androguard.core.apk import APK
apk = APK(apk_path)
version_code = apk.get_androidversion_code()
version_name = apk.get_androidversion_name()
print(f"APK解析成功: version_code={version_code}, version_name={version_name}")
return {
'version_code': int(version_code) if version_code else None,
'version_name': version_name
}
except ImportError as ie:
print(f"androguard 导入失败: {str(ie)}")
return parse_apk(apk_path)
except Exception as e:
print(f"使用androguard解析APK失败: {str(e)}")
import traceback
traceback.print_exc()
return None