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

执行adb shell am instrument命令应用终止,如何通过Shell调用自定义Instrumentation类?

解决adb instrument命令导致应用终止及自定义Instrumentation调用问题

Hey,我来帮你搞定这两个问题——应用终止和自定义Instrumentation的调用。先从排查崩溃原因说起,再给你正确的命令写法:

一、先揪出应用终止的元凶

  • 权限没配全:自定义Instrumentation需要系统权限,你得先检查AndroidManifest.xml里有没有加这行权限:
    <uses-permission android:name="android.permission.INJECT_EVENTS" />
    
    还要确保Instrumentation的声明正确,得指定目标应用包名,比如:
    <instrumentation
        android:name="com.pg.service.TestInstrumentation"
        android:targetPackage="你的目标应用包名" />
    
  • 进程冲突搞事情:有时候目标应用进程没彻底杀掉,执行instrument命令就会冲突。先强制停掉应用再试:
    adb shell am force-stop 你的目标应用包名
    adb shell am instrument -w com.pg.service/com.pg.service.TestInstrumentation
    
  • 代码里藏着小bug:看你贴的代码,moduleNameList的初始化少了泛型尖括号,应该写成new ArrayList<>(Arrays.asList(moduleList)),不然rawtypes警告可能导致隐性异常,直接让进程崩掉。另外methodNameList的代码没写完,要是这里有未捕获的异常,也会触发终止。

二、正确调用自定义Instrumentation的Shell命令

要通过adb调用你的TestInstrumentation,命令格式得精准,核心是指定测试包名和Instrumentation类的全路径:

adb shell am instrument -w [测试包名]/[Instrumentation类的全路径]

比如你的类是com.pg.service.TestInstrumentation,如果测试包名就是com.pg.service(和目标应用包名一致的话),直接用这条命令:

adb shell am instrument -w com.pg.service/com.pg.service.TestInstrumentation

要是你的测试代码是单独打包成测试APK的,记得先安装这个测试APK,而且测试APK和目标应用APK的签名必须一致,不然会被系统拦下来。

三、调试小技巧帮你快速定位问题

  • 用日志抓崩溃细节:执行这条命令,能直接过滤出应用崩溃和Instrumentation相关的日志,一眼看到哪出问题了:
    adb logcat -d | grep -E "AndroidRuntime|TestInstrumentation"
    
  • 在Instrumentation里加日志:比如在onCreate方法里加日志输出,确认代码有没有执行到这一步,方便排查逻辑问题:
    @Override
    public void onCreate(Bundle arguments) {
        super.onCreate(arguments);
        Log.d("TestInstrumentation", "onCreate方法已执行");
        // 后续逻辑写在这里
    }
    

内容的提问来源于stack exchange,提问作者fei yu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:06:24