自动化实战1:实现检查dex文件,获取方法数

背景:安卓编包有个65535规则,所有dex文件的方法数和field数值都不能超过65535,否则会导致编包失败,因此在研发过程中需要对分支包的方法数进行监控。

实现逻辑:
1.采用dexdump工具对dex文件进行解析;

dexdeump -f <path>  >1.txt

2.获取apk包路径,将其变成zip文件(zip格式文件才能实现解压获取dex文件);

传入的apk复制一份至本地目录下(避免修改到原文件),将复制后的apk利用rename()重命名为test.zip,后续对test.zip进行解压获取

3.定义函数一,实现获取dex数量

代码中定义get_dex(),利用os.system()执行dexdump命令获取方法数并返回

4.提取所要检查的dex文件,调用3

zip中有很多文件,提取出我们想要的dex文件,后续对该目录下的dex文件进行解析即可

5.输出完成后将过程中创建的文件和文件夹都删掉,避免占用空间

主要运用shutil.rmtree()对非空文件夹进行删除,os.remove()对文件进行删除

6.编译为exe文件,执行不受环境限制。

py文件运行需要python编译环境,为更加方便其他人使用,利用pyinstaller打包为exe文件。
pyinstaller -F get_dex_count.py 

代码实现:

'''
获取主dex、othermain、extlib方法数
'''
import os
import shutil
import zipfile


class GetDexCount(object):
    def get_dex(self, path):
        os.system('dexdump -f ' + path + ' > ' + '1.txt')
        dex_txt_path = os.path.abspath('1.txt')
        try:
            # apk_size = round(os.path.getsize(path)/(1024*1024),2)
            with open(dex_txt_path, 'r', encoding='utf-8')as f:
                for i in f.readlines():
                    if 'field_ids_size' in i:
                        field_count = i.split(':')[1].strip('\n')
                    elif 'method_ids_size' in i:
                        method_count = i.split(':')[1].strip('\n')
                        break
                return field_count, method_count
        except FileNotFoundError as msg:
            print(msg)

    def print_dex_count(self, apk_zip, dex_dst_dir):
        if not os.path.exists(dex_dst_dir):
            os.mkdir(dex_dst_dir)
        if zipfile.is_zipfile(apk_zip):
            apk_file = zipfile.ZipFile(apk_zip, 'r')
            for file in apk_file.namelist():
                if file in ['classes.dex', 'classes12.dex', 'classes2.dex']:
                    apk_file.extract(file, dex_dst_dir)
            try:
                for home, dirs, dex_file in os.walk(dex_dst_dir):
                    for i in dex_file:
                        dex_path = os.path.join(home, i)
                        field_count, method_count = self.get_dex(dex_path)
                        print('******'+i+'********')
                        print('field_count:'+str(field_count))
                        print('method_count:'+str(method_count))
            except FileNotFoundError as msg:
                print(msg)
        else:
            print("Not Zip")


if __name__ == '__main__':
    path = os.getcwd()
    dex_dst_dir = path + '\\dex'
    apk = input("拖入apk:")
    apk_file = shutil.copy(apk, dex_dst_dir)
    zip_file = 'test.zip'
    if os.path.exists(zip_file):
        os.remove(zip_file)
    os.rename(apk_file, zip_file) #可以重命名也可以修改路径
    GetDexCount().print_dex_count(zip_file, dex_dst_dir)
    os.remove(zip_file)
    shutil.rmtree(dex_dst_dir)
    os.remove('1.txt')
    os.system('pause')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值