I've done some more investigation and now have a slightly different understanding. The version of Gradle that you use determines the max version of java that you can use for your gradle builds. See
docs.gradle.org/current/userguide/compatibility.html. For example, I'm using gradle 7.4 and I use Java 14 to build my custom gradle plugins. The version of java that you use for your gradle builds has nothing to do with the version of java that android supports. It's the combination of the version of your android gradle plugin and your android sdk that determines what java language features you can use. Your andoid/SDK/build-tools directory contains a D8.bat file. D8 is the build tool used for converting java into dex. That batch file finds an appropriate java jdk to run the d8.jar file also contain in your build-tools directory. I'm not sure if this program takes java files as input or java class files, but I think it's the later. In my case, the java jdk it uses to run the d8.jar is a java 8 compiler. That compiler (jdk version) gets set up by Android Studio. Be aware that newer versions of Android Studio could embed a different compiler. I'm using Android Studio 2021.2.1, which is the most current version at the time of this posting. That warning message D8: "One or more classes has class file version >= 56 which is not officially supported", comes from that process. So essentially, that process seems to be saying that it ownly reliably supports java class file formats up to verion 55 which is of course Java 11. That said, android itself currently only supports Java 7 in its entirety and a subset of java 8. Here's a link to the subset of java 8 features that android supports:
Use Java 8 language features and APIs | Android Developers. That link says that in order to those java 8 features you must set sourceCompatibility and targetCompatibility to Java 1.8. My guess is that if you set those options to a higher value it treats it like you set it to 1.8; it doesn't give you any errors; and it just confuses everyone as to what that means since most of us provide configuration data by example of what others have done. Again, that's just a guess. So what that D8 program does, is to take your input and builds dex code that is compatible to run on android. So if you use, some features that are not java 7 or part of the subset of java 8 that android supports, it apparently figures out a way to do that, perhaps with the addition of some added support code which may vary depending on the verion of the SDK version you're targeting. This process is known as desugaring. I'm going to guess that you'll get some kind of error if you provide a language feature that the d8 program doesn't support. That's probably why android documentation is pretty vague on the issue of java compatibility; it depends on your configuration.