作者: | 来源:互联网 | 2023-09-14 10:16
这是一个简单的脚本,用于在我的所有设备上执行adb命令,应该在Linux和MacOsX下运行。
您可能需要使其适应您的开发环境。
#!/bin/bash # Script adb+ # Usage # You can run any command adb provide on all your current devices # ./adb+ is the equivalent of ./adb -s # # Examples # ./adb+ version # ./adb+ install apidemo.apk # ./adb+ uninstall com.example.android.apis adb devices | while read line do if [ ! "$line" = "" ] && [ `echo $line | awk '{print $2}'` = "device" ] then device=`echo $line | awk '{print $1}'` echo "$device $@ ..." adb -s $device $@ fi done
要在连接多个设备时卸载软件包,可以使用以下命令。
adb devices这将输出已连接项目的列表。
List of devices attached 1234c112fsasfl device 53fsks22323233 device 192.168.56.101:5555 device
adb -s your_device_key uninstall your_package_name 。
$ adb -s 1234c112fsasfl uninstall com.test.sample success - (if the device contains the apk with the specified package name) failure - (if the device did not contain the apk with the specified package name)
您必须编写一个多次调用adb的脚本,并在每次运行时使用-s开关指定每个连接设备的序列号。
另一种方法是使用Android Maven插件 ,它可以遍历所有连接的设备(或仅限模拟器或设备)。 请参阅Maven:我编写的完整参考书中的设备与iteteraction相关章节 。
此外,Android Maven插件的多设备交互也不适用于推送,拉取,安装和运行测试。
在JAVA中:
public class main { private final static String packageName = "com.mypackage.xxx"; public static void main(String[] args) throws IOException, InterruptedException { new main().doStuff(); } private void doStuff() throws IOException, InterruptedException { Runtime rt = Runtime.getRuntime(); String command = "adb devices -l"; Process pr = rt.exec(command); ArrayList> devices = new ArrayList>(); BufferedReader bf = new BufferedReader(new InputStreamReader(pr.getInputStream())); String l = ""; while ((l = bf.readLine()) != null) { String[] res = l.split("\\s{2,}"); if (res.length == 2) { HashMapdevice = new HashMap(); device.put("serial", res[0]); device.put("name", res[1]); devices.add(device); } } String commandUninstall = "adb -s %s uninstall %s"; for (HashMapmap : devices) { String serial = map.get("serial"); String finalCommanUnisntall = String.format(commandUninstall, serial, packageName); System.out.println(finalCommanUnisntall); Process pr2 = rt.exec(finalCommanUnisntall); BufferedReader bf2 = new BufferedReader(new InputStreamReader(pr2.getInputStream())); String l2 = ""; while ((l2 = bf2.readLine()) != null) { System.out.println(l2); } } } }
我意识到这个问题已经有了一个公认的答案,但是:
for d in $(adb devices -l | sed '1d' | sed '$d' | awk '{print $1}'); do adb -s $d uninstall your.pkg.id.here; done
子命令首先:
枚举所有连接的设备
剥离第一行
剥去最后一行
打印第一列(设备标识符)
然后外部for循环:
对于每个设备标识符
从指定的设备卸载your.pkg.id.here