Debugging During Robolectric Test Execution In Android Studio

I’ve been exploring testing in Android Studio via Robolectric and wanted to check the values of some variables in the code under test. I tried using System.out.println() to print the values, expecting that when I ran my test gradle task I’d see the output in my terminal, since I run my tests via gradle’s cli. What I got was the same test failure, but no output from my println() call.

For my next attempt, I added a breakpoint and ran the tests via the same cli command, but that didn’t work either. After a bit of searching I learned that I can debug a gradle task easily by finding the task in Android Studio’s Gradle tool window and selecting debug (you can right-click, or use the Run menu, or use the CTRL-SHIFT-D keyboard shortcut.

What I learned next is that doesn’t work right way, because by default Android Studio executes gradle via an in-process build, resulting in the following error: