错误:Error=Unable to find instrumentation info for: ComponentInfo{xxx.xxx.xxx/xxxx}INSTRUMENTATION_STATUS_CODE: -1
解决方法:产生这个问题的原因一般有两种情况

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="测试程序的包名"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <instrumentation
        android:name="pl.polidea.instrumentation.PolideaInstrumentationTestRunner"
        android:targetPackage="被测程序的包名" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

我属于情况 1
问题描述:在命令行中用 adb shell am instrument 执行测试程序会报如下错误,如果用 Eclipse 运行一次则不会再报

操作:在用命令运行执行-w com.ktplay.testing.internal/com.ktplay.testing.common.InstrumentationTestRunner2命令 测试程序报如下错误:


E:\ktplay-android-sdk\sdk\ktplay_testing\ktplay_testing_internal>adb shell am instrument -w com.ktplay.testing.internal/com.ktplay.testing.common.InstrumentationTestRunner2
INSTRUMENTATION_STATUS: id=ActivityManagerService
android.util.AndroidException: INSTRUMENTATION_FAILED: com.ktplay.testing.internal/com.ktplay.testing.common.InstrumentationTestRunner2INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.ktplay.testing.internal/com.ktplay.testing.common.InstrumentationTestRunner2}
INSTRUMENTATION_STATUS_CODE: -1

        at com.android.commands.am.Am.runInstrument(Am.java:616)
        at com.android.commands.am.Am.run(Am.java:118)
        at com.android.commands.am.Am.main(Am.java:81)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235)
        at dalvik.system.NativeStart.main(Native Method)

E:\ktplay-android-sdk\sdk\ktplay_testing\ktplay_testing_internal>

下边是 AndroidManifest.xml 文件内容

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ktplay.testing.internal"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />

   <instrumentation
        android:name="com.ktplay.testing.common.InstrumentationTestRunner2"
        android:targetPackage="com.ktplay.sample" />
 <!--       <instrumentation
        android:name="com.zutubi.android.junitreport.JUnitReportTestRunner"
        android:targetPackage="com.ktplay.sample" />
  --> 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

猜测:这个错误在命令行中运行会报,如果用 Eclipse 运行一次则不会再报,感觉是 Eclipse 帮它装了些什么似的,如果我在命令行中运行,是需要先做一些什么操作呢?

InstrumentationTestRunner2 是我自己写的一个 testrunner,如果在 Eclipse 中运行,只要配置一下 Instrumentation runner

用命令adb shell pm list instrumentation可以查看模拟器中已安装的 instrumentation packages,


C:\Users> adb shell pm list instrumentation
instrumentation:com.android.emulator.connectivity.test/android.test.InstrumentationTestRunner (target=com.android.emulator.connectivity.test)
instrumentation:com.android.emulator.gps.test/android.test.InstrumentationTestRunner (target=com.android.emulator.gps.test)
instrumentation:com.android.smoketest.tests/com.android.smoketest.SmokeTestRunner (target=com.android.smoketest)
instrumentation:com.android.smoketest.tests/android.test.InstrumentationTestRunner (target=com.android.smoketest)
instrumentation:com.example.android.apis/.app.LocalSampleInstrumentation (target=com.example.android.apis)

可以看到模拟器上默认安装了系统自带的/android.test.InstrumentationTestRunner,而没有我自己的 com.ktplay.testing.common.InstrumentationTestRunner2

解决
在测试时不仅需要安装被测试程序的 apk,同时需要将测试程序生成的 apk 同时安装到模拟器或手机上。

在 Eclipse 上 Run as Android junit test 时它自己会把测试 apk 装到模拟器上,所以可以正常运行,而用命令的时候没有安装,所以会报这个错。现在用 adb shell am instrument 查看,就会显示出我的 com.ktplay.testing.common.InstrumentationTestRunner2

C:\Users\KTplay> adb shell pm list instrumentation
instrumentation:com.android.emulator.connectivity.test/android.test.InstrumentationTestRunner (target=com.android.emulator.connectivity.test)
instrumentation:com.android.emulator.gps.test/android.test.InstrumentationTestRunner (target=com.android.emulator.gps.test)
instrumentation:com.android.smoketest.tests/com.android.smoketest.SmokeTestRunner (target=com.android.smoketest)
instrumentation:com.android.smoketest.tests/android.test.InstrumentationTestRunner (target=com.android.smoketest)
instrumentation:com.example.android.apis/.app.LocalSampleInstrumentation (target=com.example.android.apis)
instrumentation:com.ktplay.testing.internal/com.ktplay.testing.common.InstrumentationTestRunner2 (target=com.ktplay.sample)


↙↙↙阅读原文可查看相关链接,并与作者交流