Fixing `An exception has occurred in the compiler (1.8.0_242-release)`

Today my Flutter project failed to compile with the following error:

An exception has occurred in the compiler (1.8.0_242-release). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you.
java.lang.AssertionError: annotationType(): unrecognized Attribute name MODULE (class com.sun.tools.javac.util.UnsharedNameTable$NameImpl)
        at com.sun.tools.javac.util.Assert.error(Assert.java:133)
        at com.sun.tools.javac.code.TypeAnnotations.annotationType(TypeAnnotations.java:231)

A quick Google search returns those two posts:

So I ran flutter doctor and got this:

Everything looks fine except for some Android toolchain and Xcode issues, which seem inconsequential. Still, I'll update Android Studio as suggested. I opened Android Studio, clicked onto "Check Updates" and clicked on what it suggested to me. Some Android Studio Preview. After installing it, I ran flutter doctor -v again and got this:

Now it can't even find a bundled Java version...

I tried compiling again, and it gave me the same error, meaning that it's still using OpenJDK version 1.8.0_242-release.

I tried installing Oracle's JDK, but compilation does not use it.

So I went to https://jdk.java.net/18/ to download the latest build of OpenJDK. But the tar file contains just the files, not an installer. I don't want to have to read the docs on how to install it, where to unpack it, what new paths or virtual links to set up.

Another blind try, installing a few more SDK components in Android Studio.

But it did not change a thing. So I uninstalled Android Studio Preview and decided to go for the stable version at https://developer.android.com/studio. And this time it worked!

And it finally compiled my app.