android adb将内置apk删除,如何使用adb从多个连接的设备卸载APK?

这是一个简单的脚本,用于在我的所有设备上执行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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值