Print Android instrumentation tests logs in the terminal/console
While debugging some Android instrumentation tests that were ran using ‘adb shell am instrument'
, I needed to print some logs into the terminal/console to inspect the flow and resolve some issues.
One would think that you could simply use Log.i/e/d or System.out for logging into terminal. But Log.i/e/d or System.out won’t print the logs into the terminal. This forced me to look for another solution and I found something to print the logs I needed in the terminal. You can see below.
fun logToConsole(stringToPrint: String) { val bundle = Bundle() bundle.putString( Instrumentation.REPORT_KEY_STREAMRESULT, stringToPrint ) InstrumentationRegistry.getInstrumentation().sendStatus(0, bundle) }
The ideea is to get the current Instrumentation instance. Then, use the
method to print your debug message/logs in the terminal. sendStatus
Using the function above you can: print Android instrumentation tests logs into the terminal
Looking for some other Logs related article? Maybe you want to also read: “Logs – how to filter them in Android Studio“