# -*- coding: utf-8 -*-
# @Author : Pineapple Dong ^_^
# @Time : 2018/5/28 10:14
# @File : package_tool.py
import re
import subprocess
import os
import json
class ApkInfo:
def __init__(self, apk_path):
"""
通过apk包获取apk信息
:param apk_path:apk路径
"""
self.apkPath = apk_path
self.aapt_path = self.get_aapt()
@staticmethod
def get_aapt():
if "ANDROID_HOME" in os.environ:
root_dir = os.path.join(os.environ["ANDROID_HOME"], "build-tools")
for path, subdir, files in os.walk(root_dir):
if "aapt.exe" in files:
return os.path.join(path, "aapt.exe")
else:
return "ANDROID_HOME not exist"
def get_apk_size(self):
"""
得到apk的文件大小
:return:
"""
print(os.path.getsize(self.apkPath))
size = round(os.path.getsize(self.apkPath) / (1024 * 1000), 2)
return str(size) + "M"
def get_apk_base_info(self):
"""
获取apk包的基本信息
:return:
"""
p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
match = re.compile("package: name='(\S+)' versionCode='(\d+)' versionName='(\S+)'").match(output.decode())
if not match:
raise Exception("can't get packageinfo")
package_name = match.group(1)
version_code = match.group(2)
version_name = match.group(3)
# print('packageName:' + package_name)
# print('versionCode:' + version_code)
# print('versionName:' + version_name)
return package_name, version_name, version_code
def get_apk_name(self):
"""
获取apk名字
:return:
"""
p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
t = output.decode().split("\n")
for item in t:
# 此处的apk包名我是取得中文名称。具体信息可以在dos下用aapt查看详细信息后,修改正则获取自己想要的name
match = re.compile("application-label-zh-CN:'([\u4e00-\u9fa5_a-zA-Z0-9-\S]+)'").search(item)
if match is not None:
return match.group(1)
def get_apk_activity(self):
"""
得到启动类
:return:
"""
p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
match = re.compile("launchable-activity: name='(\S+)'").search(output.decode())
if match is not None:
return match.group(1)
if __name__ == '__main__':
apk_info = ApkInfo(r"D:\Backup\桌面\work\webdf\bqs_df.apk")
# apk_info = ApkInfo(r"D:\ChromeDownloads\suishouji_1059000.apk")
print(apk_info.get_apk_activity())
print(apk_info.get_apk_base_info())
print(apk_info.get_apk_name())
print(apk_info.get_apk_size())
使用aapt获取apk包的详细信息(python的demo)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 20- 枚举,枚举原始值,枚举相关值,switch提取枚举关联值 Swift枚举: Swift中的枚举比OC中的枚...
- 昨天奔跑吧兄弟第五季开更了 里面怀念90年代义务的那个怀旧画面 野郎特别的喜欢 可能是因为和我小的时候的生活环境相...