"java.net.BindException: Address already in use" when running tests

When running my Java test with gradle (./gradlew test), I often get an error “java.net.BindException: Address already in use”. If I set ‘test { forkEvery = 1 }’, I get the error every time.

I pasted the stack trace, build.gradle and debug log here.

Is there any way to debug this issue? What ports are being used?

I’m using Gradle 1.6.

Turns out it’s not Gradle’s fault - it’s Android Studio using up just about every port on my machine.