python获取安卓包名与activity方法

代码如下:

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

你可能感兴趣的:(python获取安卓包名与activity方法)