Today wrote a script that uses Python to automatically batch install APK to the device, the implementation process is as follows:
1. First, you need to read the information about the APK from the XML file:
The XML file structure is defined as follows:
<?XML version= "1.0"?><Data> <APKs> <apkID= ' 0001 '>test01.apk</apk> <apkID= ' 0002 '>test02.apk</apk> </APKs></Data>
Use ElementTree to parse the XML file node, where the description of Apk.text is the name of the apk that we need to install, first to a list;
Import
Tree = et.parse ('apklist.xml'= tree.getroot () applist= [] for in Root.iter ('apk'): print apk.tag , Apk.attrib, Apk.text applist.append (apk.text)print"====app list===="
Print
=====================================================================================Result:apk {' ID'0001'} test01.apkapk {'ID' '0002'} test02.apk
2. On the basis of obtaining the APK list, we need to know the Android device information connected to this computer, the Genymotoin virtual machine used here, if the real machine or Google's emulator, the parameters will be slightly different:
Use the command commands of the ADB devices to get the parameters and, based on the obtained results, a slight correction to get the final devices List
cmd = r'ADB devices'devlist= subprocess. Popen (cmd, shell=true, stdout=subprocess. PIPE, stderr=subprocess. STDOUT) Devices= [] forLineinchDevList.stdout.readlines () [1:]: if 'List' not inchline:devices.append ((Line.split ('\ t') ) [0]) Devices.pop ()Print "===devices==="PrintDevices=====================================================================================Result:
===devices===
[' 192.168.56.101:5555 ']
3. You have obtained the relevant APK list and device list, the next step is to install the steps, using the ADB install compared to push to be simple and convenient:
print " ===install apk=== " for Dev in devices: for apk in applist:installcmd = R " adb-s%s install-r. \%s " % (Dev, apk) print installcmd result = subprocess. Popen (Installcmd, Shell=true, stdout=subprocess. PIPE, Stderr=subprocess. PIPE) print (Result.stdout.readlines ())
====================================================================================
Result:
===install apk===
Adb-s 192.168.56.101:5555 install-r. \test01.apk
[' \tpkg:/data/local/tmp/test01.apk\r\r\n ', ' success\r\r\n ']
Adb-s 192.168.56.101:5555 install-r. \test02.apk
[' \tpkg:/data/local/tmp/test02.apk\r\r\n ', ' success\r\r\n ']
Two loops were used to complete the input of the device and the APK params, and found in the results "Success" keyword ~ install okay~
4. Finally, to see the results of the installation, use the logging module to get the installation steps and change the code portion of print to logging
Import logging
Logging.basicconfig (level=logging. DEBUG, Format='% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s', Datefmt='%a,%d%b%Y%h:%m:%s', filename='Installer.log', FileMode='W') Logging.info ("start to install apk")
===================================================================================
Result:
Thu, 23:23:48 Installapk.py[line:10] INFO start to install APK
Summary: Python logging module is really powerful + easy to use ~, tomorrow to study the principles of logging and api~
Use Python to install apk in bulk to your device