使用bat脚本调用py文件直接获取应用的包名和targetversion
Posted reseelei-despair
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用bat脚本调用py文件直接获取应用的包名和targetversion相关的知识,希望对你有一定的参考价值。
背景:
在上一篇已经介绍过如何利用python调用aapt获取包名
https://www.cnblogs.com/reseelei-despair/p/11078750.html
但是因为每次都要修改py文件内的安装包所在路径,感觉不是很方便,所以想办法用bat脚本实现直接将文件拖入bat文件直接运行获取到相关信息
思路:
批处理脚本直接运行py文件,修改py文件,导入sys模块,直接在cmd中将安装包路径传入py后运行
代码:
bat脚本代码如下
@echo off COLOR 2 set "getname=%1" echo %getname% python D:\\Pyexerice\\aapt_badging.py %getname% pause
使用方法:创建一个txt文件,代码复制进去,保存后退出,将文件后缀名修改成bat即可
python代码修改部分
# -*- coding: utf-8 -*- import re import subprocess import os import sys#导入sys模块 class ApkInfo: def __init__(self, apk_path): 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_base_info(self): 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+)‘").match(output.decode()) if not match: raise Exception("can‘t get packageinfo") package_name = match.group(1) return package_name def get_apk_activity(self): 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) def get_apk_sdkVersion(self): 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("sdkVersion:‘(\\S+)‘").search(output.decode()) return match.group(1) def get_apk_targetSdkVersion(self): 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("targetSdkVersion:‘(\\S+)‘").search(output.decode()) return match.group(1) if __name__ == ‘__main__‘: apk_info = ApkInfo(sys.argv[1])#直接将cmd中给的参数引用至此 print("Activity:%s"%apk_info.get_apk_activity()) print("apkName:%s"%apk_info.get_apk_base_info()) print("sdkVersion:%s"%apk_info.get_apk_sdkVersion()) print("targetSdkVersion:%s"%apk_info.get_apk_targetSdkVersion())
效果:
将文件拖入
运行结果如下
以上是关于使用bat脚本调用py文件直接获取应用的包名和targetversion的主要内容,如果未能解决你的问题,请参考以下文章