摘要
本文是藉助sdk裏的工具aapt.exe獲取apk信息,所獲取apk獲取信息爲字符串,可以使用正則表達式從中獲得apk版本號等想要的信息
系統環境:windows
運行環境:python2.7
依賴工具:aapt.exe
(”aapt.exe” 存放於文件夾”\build-tools\android-4.4.2\”下,下載地址爲:https://android-sdk.en.softonic.com/)
相關代碼
#!/usr/bin/env python
#coding:utf-8
import os,re
#檢查apk版本號等信息
def getAppBaseInfo(parm_aapt_path, parm_apk_path):
get_info_command = "%s dump badging %s" % (parm_aapt_path, parm_apk_path) #使用命令獲取版本信息 aapt命令介紹可以相關博客
output = os.popen(get_info_command).read() #執行命令,並將結果以字符串方式返回
match = re.compile("package: name='(\S+)' versionCode='(\d+)' versionName='(\S+)'").match(output) #通過正則匹配,獲取包名,版本號,版本名稱
if not match:
print output
raise Exception("can't get packageinfo")
packagename = match.group(1)
versionCode = match.group(2)
versionName = match.group(3)
print u" 包名:%s \n 版本號:%s \n 版本名稱:%s " % (packagename, versionCode, versionName)
if __name__=='__main__':
path = os.path.abspath(os.path.dirname(__file__)) + "\\"
aapt_path = path + "tools\\aapt.exe" #解析工具aapt.exe地址
apk_path = path + "publish_files\\TrunkingHome.apk" #apk地址
getAppBaseInfo(aapt_path, apk_path)