android.util.AndroidException: INSTRUMENTATION_FAILED:

前端 未结 3 1325
星月不相逢
星月不相逢 2021-01-31 09:57

I have a simple android app and I am testing it using my phone. So, there are two ways to do that :

  1. Using eclipse
  2. Using CLI

Problem:

3条回答
  •  既然无缘
    2021-01-31 10:21

    I suppose that you will have solved it since january, but I work with command-line tools, found similar problem (error message is different) and solved it like I explain in following steps. I do the whole process from creating a dummy project with its empty test until a successful test run. I hope it can be useful for someone:

    First step, create the project:

    android create project 
      --name MyExample 
      --target "Google Inc.:Google APIs:17" 
      --path MyExample 
      --package com.example 
      --activity MyExampleActivity
    

    Second step, create test project:

    android create test-project 
      --path MyExampleTest 
      --name MyExampleTest 
      --main ../MyExample
    

    Third step, access to your project directory, build it and check that the process ends successfully:

    cd MyExample && ant debug
    

    Fourth step, install it to the emulator:

    adb -s emulator-5554 install -r bin/MyExample-debug.apk
    

    Fifth step, access to your test project directory and try to run the tests:

    cd ../MyExampleTest && 
    adb shell am instrument -w com.example.tests/android.test.InstrumentationTestRunner
    

    That yields:

    INSTRUMENTATION_STATUS: id=ActivityManagerService
    INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.example.tests/android.test.InstrumentationTestRunner}
    INSTRUMENTATION_STATUS_CODE: -1
    android.util.AndroidException: INSTRUMENTATION_FAILED: com.example.tests/android.test.InstrumentationTestRunner
            at com.android.commands.am.Am.runInstrument(Am.java:676)
            at com.android.commands.am.Am.run(Am.java:119)
            at com.android.commands.am.Am.main(Am.java:82)
            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)
    

    Sixth step, list your instrumentation clases and ensure that your current project is missing:

    adb shell pm list instrumentation
    

    That in my machine yields:

    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.example.spinner.tests/android.test.InstrumentationTestRunner (target=com.android.example.spinner)
    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)
    

    As you can see, the instrumentation for com.example.tests doesn't exist, so we will have to create it.

    Seventh step, build you test project and check that it did successfully:

    ant debug
    

    Eigth step, install it to the emulator:

    adb -s emulator-5554 install -r bin/MyExampleTest-debug.apk
    

    Ninth step, list your instrumentation classes and look for the one of your project:

    adb shell pm list instrumentation
    

    That yields:

    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.example.spinner.tests/android.test.InstrumentationTestRunner (target=com.android.example.spinner)
    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.tests/android.test.InstrumentationTestRunner (target=com.example)
    instrumentation:com.example.android.apis/.app.LocalSampleInstrumentation (target=com.example.android.apis)
    

    Look at the second to last, instrumentation:com.example.tests, it's which we wanted.

    Tenth step, run your tests:

    adb shell am instrument -w com.example.tests/android.test.InstrumentationTestRunner
    

    That yields:

    Test results for InstrumentationTestRunner=
    Time: 0.0
    
    OK (0 tests)
    

    That is all. Now implement your tests, compile and install as usual. Additionally you can remove them like:

    adb shell pm uninstall com.example.tests
    

    But you will need to create instrumentation classes again to avoid the same error.

提交回复
热议问题