How do I configure Java for Cordova and Gradle on Ubuntu?

When I install Gradle on Ubuntu, it installs JDK11 but then Cordova complains. So I replace JDK11 with JDK8 and Gradle doesn't work. This includes changing the JAVA_HOME environmental variable. I would have thought that sudo npm install -g cordova should have taken care of all of this. IMO this should have been as simple as a short command with a package manager, but instead I find myself in dependency hell.

Currently, when I run cordova run android in the path of my project, I get the following:

Android Studio project detected
cordova-android-support-gradle-release: Android platform: V7+
cordova-android-support-gradle-release: Wrote custom version '27.+' to <path stuff>
cordova-android-support-gradle-release: Wrote custom version '27.+' to <path stuff>
Failed to run "javac -version", make sure that you have a JDK installed.
You can get it from: http://www.oracle.com/technetwork/java/javase/downloads.
Your JAVA_HOME is invalid: /usr/lib/jvm/java-11-openjdk-amd64

How exactly do I install Cordova with all dependencies and versions on Ubuntu? I'm looking for a complete and step-by-step set of instructions.