Python安装apk

#_*_ coding:utf-8 _*_

import os
import time
import subprocess
from nt import chdir

COMMEND1 = 'aapt dump badging '
COMMEND2 = ' | findstr package'
PREPATH = os.path.abspath('.') + '/app/'

#安装APK
def InstallAPK(ff):
    print "adb install -r " + ff
    text = os.popen("adb install -r " + ff)
    time.sleep(2)
    print text.read()
    return ff

#解析APK包,获取包名
def GetApkName(commond):
    p = subprocess.Popen(commond, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True)
    #print p
    (output, err) = p.communicate()
    if output != '':
        try:
            tempresult = output[15:]
            result = ''
            for tempstr in tempresult:
                result = result + tempstr
                #print result
                if tempstr == "'":
                    break
            return result[:-1]
        except Exception, e:
            return ""
    return ''

#更改文件夹内所有文件名称
def renameAPK(preName, newName):
    chdir(os.path.dirname(preName))
    os.rename(preName, newName)

'''
1.通过AAPT工具获取到当前APK包的包名;
2.将获取到的包名更新到当前APK;
3.安装APK到手机
'''
if __name__ == "__main__":
    APPFailes = os.listdir(PREPATH)
    print PREPATH
    for files in APPFailes:

        commond = COMMEND1 + PREPATH + files + COMMEND2
        strApkName = GetApkName(commond)
        print strApkName

        #更名需要知道新文件和旧文件的完整路径
        filePre = PREPATH + files
        fileNew = PREPATH + strApkName + '.apk'

        renameAPK(filePre, fileNew)
        InstallAPK(fileNew)

猜你喜欢

转载自blog.csdn.net/github_35707894/article/details/79626142