I tried all the top solutions, but they didn't work. Although the error message was the same, the issue might have been different.
My solution was to change the Gradle version in the build tools (Settings -> Build, Execution, Deployment -> Build Tools -> Gradle), as the previous one (Gradle JDK Version) was likely causing the error due to potential JDK permission issues that I hadn't granted. After switching the Gradle JDK to a different version, I rebuilt the project, and it successfully compiled and ran again.