tl;dr: downgrade java by running update-alternatives
My system gradle version was 4.4.1, and the gradle wrapper version was 4.0. After running the command given by several other answers:
gradle wrapper --gradle-version 4.4.1
I still had the same error:
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine java version from '11.0.4'.
It turns out java 11 wasn't supported until gradle 4.8, and my software repositories only had 4.4.1. (Also, upgrading to newer gradle version might have been incompatible with the package I was trying to compile.)
The answer was to downgrade java. My system actually had java8 already installed, and it was easy to switch between java versions by running this command and following the instructions:
sudo update-alternatives --config java