温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python获取安卓包名与activity方法

发布时间:2020-09-10 09:48:43 来源:网络 阅读:594 作者:知止内明 栏目:编程语言

代码如下:


def adbcmdapk(filepath):
    if not os.path.exists(filepath):
        return 0
    getPackageActivity = 'aapt d badging %s |findstr "package launchable-activity"' % (filepath)
    result = os.popen(getPackageActivity).readlines()
    packageandpackage = {}
    package = ""
    activity = ""
    for i in range(len(result)):
        if "package" in result[i]:
            nameNumber = result[i].find("e='")
            versionCodeNumber = result[i].find("' v")
            package = result[i][nameNumber + 3:versionCodeNumber]
        if "launchable-activity" in result[i]:
            nameNumber = result[i].find("e='")
            versionCodeNumber = result[i].find("'  l")
            activity = result[i][nameNumber + 3:versionCodeNumber]
        # 添加到字典中
        packageandpackage[package] = activity
    return packageandpackage

测试类


if __name__ == '__main__':
    pa = "E:\\toontest3.8.1.apk"
    print(adbcmdapk(pa))

结果:

{'com.systoon.toon': 'com.systoon.toon.user.login.view.WelcomeActivity'}

第二种方式:

class ApkInfo():
    def __init__(self, apkPath):
        self.apkPath = apkPath

     def getApkBaseInfo(self):
        p = subprocess.Popen("aapt 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")
        packagename = match.group(1)
        versionCode = match.group(2)
        versionName = match.group(3)

        print('packagename:' + packagename)
        print('versionCode:' + versionCode)
        print('versionName:' + versionName)
        return packagename, versionName, versionCode

if name == 'main':
ApkInfo(r"E:\toontest3.8.1.apk").getApkBaseInfo()

结果:

packagename:com.systoon.toon
versionCode:1803060450
versionName:3.8.1

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI