作者個人網(wǎng)站:http://www.harddone.com
說明
本文以具體的python腳本為例進(jìn)行說明朴乖,兩個腳本都是根據(jù)開發(fā)過程中的業(yè)務(wù)需求而開發(fā),并不具備通用性助赞。但是其核心代碼都是通過python的api執(zhí)行adb命令:
//example
cmd = 'adb shell'
os.system(cmd)
uninstall_clean_app.py
#!/usr/bin/python
import subprocess
import os, sys
import getopt
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
if __name__ == '__main__':
""" change commands and add shell"""
tag = ''
try:
opt, args = getopt.getopt(sys.argv[1:], "ht:", ['pkg', 'help'])
for op, value in opt:
if op in ("-t", "--pkg"):
tag = value
if op in ("-h", "--help"):
print "Usage: main_app_clean.py -t APP_PKG_NAME"
print "Options:"
print " -t APP_PKG_NAME should be a bundle id !"
print ""
print "Sample : ./main_app_clean.py -t <bundle id>"
print ""
sys.exit()
except getopt.GetoptError:
print "Error: Could not find the args."
print "Usage: main_app_clean.py -t APP_PKG_NAME"
print "Options:"
print " -t APP_PKG_NAME should be a bundle id !"
print ""
print "Sample : ./main_app_clean.py -t <bundle id>"
print ""
sys.exit()
if tag == '':
print "you should input a bundle id !"
exit()
pkg = tag
print ''
print '1) uninstalling ' + pkg +' ...'
unInstallCmd = 'adb uninstall ' + pkg
os.system(unInstallCmd)
print ''
print '2) cleaning the cached file...'
cleanCmd1 = 'adb shell rm -fR /sdcard/.DataBackupTest'
os.system(cleanCmd1)
cleanCmd2 = 'adb shell rm -fR /sdcard/.DataBackup'
os.system(cleanCmd2)
print ''
print ' All done !^_^!'
print ''
exit()
根據(jù)app bundle id 卸載應(yīng)用买羞,并且刪除該應(yīng)用在sdcard目錄下生成的文件夾等。當(dāng)前腳本中所刪除的目錄是寫死的雹食,可以根據(jù)需要自己修改畜普。
-
使用方法
- 下載腳本到指定目錄
- 打開terminal,執(zhí)行cd命令到腳本所在目錄
- 執(zhí)行python命令
python ./uninstall_clean_app.py -t com.xxx.app
obb_push.py
#!/usr/bin/python
import subprocess
import os, sys
import getopt
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
if __name__ == '__main__':
""" change commands and add shell"""
tag = ''
try:
opt, args = getopt.getopt(sys.argv[1:], "ht:", ['tag', 'help'])
for op, value in opt:
if op in ("-t", "--tag"):
tag = value
if op in ("-h", "--help"):
print "Usage: obb_push.py -t TAG_NAME"
print "Options:"
print " -t TAG_NAME.Choose what you want to use tag, should be a obb file path !"
print ""
print "Sample : ./obb_push.py -t <obb file path>"
print ""
sys.exit()
except getopt.GetoptError:
print "Error: Could not find the args."
print "Usage: obb_push.py -t TAG_NAME"
print "Options:"
print " -t TAG_NAME.Choose what you want to use tag, should be a obb file path !"
print ""
print "Sample : ./obb_push.py -t <obb file path>"
print ""
sys.exit()
if tag == '':
print "you should input a obb file\'s path !"
exit()
print '======to get package name=======>'
obbFilePath = tag
if obbFilePath == '':
print 'you should input a obb file\'s path !'
exit()
obbSubDirs = obbFilePath.split('/')
# index = len(obbSubDirs) - 1
obbFileName = obbSubDirs[-1]
print '>>>obbFileName = ' + obbFileName
if obbFileName == '' or obbFileName.find('.obb') == -1:
print 'can not find a obb file in the path !'
exit()
tmpPackageName = obbFileName.split('.')
print tmpPackageName
packageName = ''
# for com in tmpPackageName[2:-2]:
# print com
# if com == tmpPackageName[-2]:
# packageName += com
# else:
# packageName += com + "."
packageName = '.'.join(tmpPackageName[2:-1])
print '>>>package name = ' + packageName
print '=======adb shell mkdir ========>'
obbDestPath = 'sdcard/Android/obb/' + packageName
subDir = ''
subDirs = obbDestPath.split('/')
for dir in subDirs:
subDir += '/' + dir
# print subDir
os.system('adb shell mkdir ' + subDir)
print '=======adb push obb file to device ========>'
pushCmd = 'adb push ' + obbFilePath.replace(' ','\\ ')+ ' /' + obbDestPath + '/'
# print pushCmd
os.system(pushCmd)
exit()
該腳本作用是根據(jù)傳入的obb文件完整路徑群叶,解析出app bundle id,然后將obb文件push到android設(shè)備上吃挑,減少出錯機(jī)會和煩人的拷貝工作,執(zhí)行該腳本可有快速完成街立。
- 使用方法同上舶衬,最后執(zhí)行命令
python ./obb_push.py -t <obb file path>