aapt 获取Android版本信息和图标

aapt android开发工具的一个命令行工具。
apk android的安装文件

下面是用python脚本获取版本 图标信息。O(∩_∩)O 有个问题呢,但是 ,先记录下。

#!/usr/bin/env python
#coding:utf-8

import os,re,sys
import zipfile

#检查apk版本号等信息
def getAppBaseInfo(apkFile, dstPath):  
    get_info_command = "aapt dump badging %s" % ( apkFile )   
    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)

    launchableActivity =""
    appCnName =""
    appEnName = ""
    
    lines = output.splitlines()
    for l in lines:
        l = l.strip()
        if l.startswith("application-icon"):
            # print(l)
            iconInfo = l.split(":")
            if os.path.splitext(iconInfo[1])[1].startswith('.xml'):
                getIconFromXml(apkFile,dstPath,iconInfo)
            if os.path.splitext(iconInfo[1])[1].startswith('.png'):
                pngPath = iconInfo[1]
                pngPath = pngPath.replace("\'","")
                iconInfo[1] = pngPath
                getIconFromPng(apkFile,dstPath,iconInfo)
        if l.startswith("launchable-activity"):
            launchableActivity = l.split("'")[1]
            lableName = l.split("'")[3]
            if appCnName == "":
                appCnName = lableName
            if appEnName == "":
                appEnName = lableName
        if l.startswith("application-label-zh-CN:") or l.startswith("application-label-zh:"):
            appCnName = l.split("'")[1]
            # print(l)
        if l.startswith("application-label-en"):
            appEnName = l.split("'")[1]
    apkInfo = u" 中文名:%s   \n 英文名:%s  \n 包名:%s \n 版本号:%s \n 版本名称:%s \n 启动类:%s " % (appCnName,appEnName,packagename, versionCode, versionName,launchableActivity)
    print(apkInfo)
    writeInfo(dstPath,apkInfo)
# 将apk的信息写入文本文件中
def writeInfo(dstPath,apkInfo):
    with open(dstPath+"/info.txt",'w') as f:
        f.write(apkInfo)

# 从apk中提取出PNG图片
def getIconFromPng(apkFile,dstPath,iconInfo):
    z = zipfile.ZipFile(apkFile,'r')
    icon = z.read(iconInfo[1])            
    with open('%s/%s.png'%(dstPath,iconInfo[0]),'wb') as f:
        f.write(icon)
#通过aapt查询图片
def getIconFromXml(apkFile,dstPath,iconInfo):
    query_xml_command = "aapt dump xmltree %s %s" % ( apkFile ,iconInfo[1])
    print(query_xml_command)
    output = os.popen(query_xml_command).read()
    
    lines = output.splitlines()
    pngResId = ''
    for l in lines:
        l = l.strip()
        if(l.startswith('A: android:drawable')):
            pngResId = l.split("@0x")[1]
    print(pngResId)
    query_png_command = "aapt dump --values resources %s | grep -iC10 %s" % ( apkFile ,pngResId)
    print(query_png_command)
    output = os.popen(query_png_command).read()
    lines = output.splitlines()
    pngPath = ""
    for l in lines:
        l = l.strip()
        if(l.startswith("(string8) ")):
            pngPath = l
            print("png l "+ l)
            pngPath = pngPath.replace("(string8) ","")
            pngPath = pngPath.replace("\"","")
            print("png path "+ pngPath)
            if os.path.splitext(pngPath)[1].startswith('.png'):
                break
            
    iconInfo[1]=pngPath
    getIconFromPng(apkFile,dstPath,iconInfo)

if __name__=='__main__':
    
    if len(sys.argv)<3:
        print('format is python3 abc.py xxx.apk /extrat/to/dir ')
        exit(0)
    apkFile = sys.argv[1]
    dstPath = sys.argv[2]

    if not os.path.splitext(apkFile)[1] == '.apk':
        print('please input a xxx.apk')
        exit(0)
    if not os.path.isfile(apkFile):
        print(apkFile+'  not found')
        exit(0)
    if  not os.path.exists(dstPath):
        os.makedirs(dstPath)
    
    getAppBaseInfo(apkFile, dstPath)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值