You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Appium中运行非Shell环境的adb push命令?

问题

我参考Appium客户端执行adb shell命令的示例代码:

Map<String, Object> args = new HashMap<>();
args.put("command", "echo");
args.put("args", Lists.newArrayList("arg1", "arg2"));
String output = driver.executeScript("mobile: shell", args);
assert output.equals("arg1 arg2");

尝试用类似方式执行adb push命令:

Map<String, Object> args = new HashMap<>();
args.put("command", "push");
args.put("args", Lists.newArrayList("./app/images.zip", "/sdcard/Android/data/com.example.demoapp/files/images.zip"));
String output = driver.executeScript("mobile: shell", args);
assert output.equals("arg1 arg2");

但adb push不属于设备shell环境支持的命令,我知道要修改driver.executeScript里的"mobile: shell"参数,但不清楚具体应该改成什么。

解决方案

你需要使用Appium专门提供的mobile: pushFile命令来实现adb push的功能,而不是mobile: shell——因为adb push是adb客户端直接执行的命令,并非设备shell环境的内置命令。

正确的代码示例如下:

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

// 读取本地文件并转换为Base64编码
byte[] fileBytes = Files.readAllBytes(Paths.get("./app/images.zip"));
String base64EncodedFile = Base64.getEncoder().encodeToString(fileBytes);

Map<String, Object> pushArgs = new HashMap<>();
// 设置设备上的目标路径
pushArgs.put("remotePath", "/sdcard/Android/data/com.example.demoapp/files/images.zip");
// 设置本地文件的Base64编码内容
pushArgs.put("payload", base64EncodedFile);

// 执行文件推送命令
driver.executeScript("mobile: pushFile", pushArgs);

说明

  • remotePath:指定文件要推送到设备上的绝对路径
  • payload:本地文件内容的Base64编码字符串,Appium会自动将其解码并写入设备指定路径

内容的提问来源于stack exchange,提问作者Rajdeep Dodiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 05:10:54