How to start Instrumentation project programmatically using Android Intent?-ThrowExceptions

Exception or error:

one way to start testcase is,

adb shell am instrument 

i want to start this using Android code (with intent)

for example,

adb shell am start -n

we can run using Android intent by following method :-

Intent intent = new Intent(, ContactsActivity.class);

But, how to run

adb shell am instrument 

by Android intent ?

Thanks for your help in advance 🙂

How to solve:

Command to start instrumentation from adb shell :-

adb shell am instrument -w   

Android Code to start instrumentation from Android Activity :-

 startInstrumentation(new ComponentName("", "android.test.InstrumentationTestRunner"), null, null);

Note :

Other Method,

Android Code for start instrumentation (Android 2.3 to Android 4.1.2)

String str_cmd = "adb shell am instrument -w";

for Android 4.2 it requires permission “android.permission.INJECT_EVENTS” & which is only allowed by System application. User application can not use this permission because of some security reasons.

so you can not use Runtime.getRuntime().exec(str_cmd); for Android 4.2 onwards …

so now working method is :

 startInstrumentation(new ComponentName("", "android.test.InstrumentationTestRunner"), null, null);

execute this command from your Activity.



This is actually not possible to do, the reason being is that to run instrumentation you need to it via ADB, adb has certain special privileges because of security and therefore can not be run on the phone (As with anything open source, it is of course possible but you would have to rewrite some android and then it would only work on phones you installed that on!).

May I ask your reason for doing this? If you really need to automate across applications your better choice might be to either us the new android ui test framework or to test only on the emulator and use something that runs on top of the view hierarchy because trying what you are currently is a dead end.

Leave a Reply

Your email address will not be published. Required fields are marked *