前言 碌者勞其心力,懶人使用工具。程式員作為懶人推動社會進步,有目共睹。 adb 已提供了開發者可以使用的全部工具,但是重覆執行一系列adb命令也令人心煩,所以,如果業務需求固定,直接在python腳 本執行adb命令。 核心代碼很簡單 cmd = 'adb shell' os.system(cmd ...
前言
碌者勞其心力,懶人使用工具。程式員作為懶人推動社會進步,有目共睹。
adb 已提供了開發者可以使用的全部工具,但是重覆執行一系列adb命令也令人心煩,所以,如果業務需求固定,直接在python腳
本執行adb命令。
核心代碼很簡單
cmd = 'adb shell' os.system(cmd)
1.業務需求: 執行應用卸載及刪除指定目錄
Python學習交流Q群:906715085### #!/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()
使用方法:
打開terminal cd <uninstall_clean_app.py dir path> 執行python命令 python ./uninstall_clean_app.py -t com.xxx.app
2.業務需求:推送obb文件到android設備並關聯到對應的應用
Python學習交流Q群:906715085##3 #!/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""" path = '' package = '' obbVersion = '' #see: https://blog.csdn.net/chengxuyuanyonghu/article/details/54972854 try: opt, args = getopt.getopt(sys.argv[1:], "hf:p:v:", ['file=', 'package=','obbVersion=','help']) for op, value in opt: if op in ("-f", "--file"): path = value if op in ("-p", "--package"): package = value if op in ("-v", "--obbVersion"): obbVersion = value if op in ("-h", "--help"): print "Usage: obb_push.py -f obb/file/full/path -p com.example.app.bundleid -v app_vesion" print "Options:" print " <-f> <-p> <-v> should not be null !" print "" print "OR: <--file=> <--package=> <-obbVersion=> should not be null " print "" print "Sample : ./obb_push.py -f obb/file/full/path.obb -p <com.example.app.bundleid> -v 15" print "" print "OR : ./obb_push.py --file=obb/file/full/path.obb --package=com.example.app.bundleid --obbVersion=15" print "" sys.exit() except getopt.GetoptError: print "Usage: obb_push.py -f obb/file/full/path -p com.example.app.bundleid -v app_vesion" print "Options:" print " <-f> <-p> <-v> should not be null !" print "OR:" print " <--file=> <--package=> <-obbVersion=> should not be null " print "" print "Sample : ./obb_push.py -f obb/file/full/path.obb -p <com.example.app.bundleid> -v 15" print "" print "OR : ./obb_push.py --file=obb/file/full/path.obb --package=com.example.app.bundleid --obbVersion=15" print "" sys.exit() if path == '': print "you should input a obb file\'s path !" exit() print '\n' print '||--------------------------------------------------------------||' print '\n' print 'NOTICE:' print 'obb file name rule: [main.bundleVersionCode.bundleID.obb]' print '\n' print '||--------------------------------------------------------------||' print '\n' print 'Start to copy obb file >>>>>>>>> ' print ' (1)===============> parsing obb file name:' obbFilePath = path if obbFilePath == '': print 'you should input a obb file\'s path !' exit() obbSubDirs = obbFilePath.split('/') # index = len(obbSubDirs) - 1 obbFileName = obbSubDirs[-1] print obbFileName if obbFileName == '' or obbFileName.find('.obb') == -1: print 'can not find a obb file in the path !' exit() print '\n' print ' get package name = ' + package print '\n' print ' (3)===============> adb shell mkdir :' obbDestPath = 'sdcard/Android/obb/' + package subDir = '' subDirs = obbDestPath.split('/') for dir in subDirs: subDir += '/' + dir # print subDir os.system('adb shell mkdir ' + subDir) print '\n' print ' (4)===============> adb push obb file to device :' pushCmd = 'adb push ' + obbFilePath.replace(' ','\\ ')+ ' /' + obbDestPath + '/' # print pushCmd os.system(pushCmd) print '\n' print ' (5)===============> adb push rename obb file:' newObbFileName = "main."+ obbVersion+"." + package + ".obb" oldFileFullPath = '/' + obbDestPath + '/' + obbFileName newFileFullPath = '/' + obbDestPath + '/' + newObbFileName print ' old:' + oldFileFullPath reameCmd = 'adb shell mv ' + oldFileFullPath + " " + newFileFullPath os.system(reameCmd) print ' new:' + newFileFullPath print '\n' print ' (6)===============> Completed!!!' print '\n' exit()
使用方法:
python </path/obb_push.py> -p <app package name > -f </path/of/obbfile> -v <app version code>
最後
今天給大家分享的刪除Android應用集文件夾到這裡就結束了,這不得給我一個贊。當然,記得收藏起來慢慢學習,有問題的小伙
伴記得評論留言,我看見都會回覆的。