Print Android test log into 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.

Log.i/e/d or System.out are not printing the logs in the terminal. I had to look for another solution and I found one over the internet(see below).

fun logToConsole(stringToPrint: String) {
        val bundle = Bundle()
            Instrumentation.REPORT_KEY_STREAMRESULT, stringToPrint
        InstrumentationRegistry.getInstrumentation().sendStatus(0, bundle)

Using the function above helps printing logs into the console or terminal you are using to run the instrumentation test.