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

如何在adb shell的Bash命令中正确传入带逗号的annotationsString参数

解决Bash中adb shell传递带逗号参数的问题

核心问题根源

Bash环境下,未正确包裹的变量会被分词处理;同时adb shell会将接收到的命令传递到Android系统的shell环境(如mksh)中二次解析,双重解析导致带逗号的参数被错误拆分。

可行解决方案

1. 变量引用时加双引号(基础通用解法)

在调用变量时用双引号包裹,确保Bash将整个带逗号的字符串作为单个参数传递给adb:

annotationsString="com.hh.tests.utils.ONE_ANNOTATION,com.hh.tests.utils.TWO_ANNOTATION"
adb shell am instrument -w -e annotations "$annotationsString" com.hh.tests.test/androidx.test.runner.AndroidJUnitRunner

关键是"$annotationsString"的写法,双引号会阻止Bash对变量内容做分词处理,完整保留逗号和整个字符串结构。

2. 包裹整个adb shell命令(应对Android端shell解析问题)

如果Android系统的shell仍会拆分参数,可将整个要执行的命令作为单个字符串传递,内部用单引号包裹带逗号的参数:

annotationsString="com.hh.tests.utils.ONE_ANNOTATION,com.hh.tests.utils.TWO_ANNOTATION"
adb shell "am instrument -w -e annotations '$annotationsString' com.hh.tests.test/androidx.test.runner.AndroidJUnitRunner"

这种写法避免了Android端shell对参数的二次拆分,确保带逗号的字符串完整传递到instrument命令中。

3. 提前转义逗号(备选方案)

也可以在定义变量时对逗号进行转义,不过可读性稍弱:

annotationsString="com.hh.tests.utils.ONE_ANNOTATION\,com.hh.tests.utils.TWO_ANNOTATION"
adb shell am instrument -w -e annotations "$annotationsString" com.hh.tests.test/androidx.test.runner.AndroidJUnitRunner

验证参数是否传递正确

可以先用echo命令快速验证:

adb shell echo "$annotationsString"
# 或
adb shell "echo '$annotationsString'"

如果输出是完整的带逗号字符串,说明参数传递正常,再替换为实际的自动化测试命令即可。

内容的提问来源于stack exchange,提问作者Red Cape

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 16:57:04