Using NVM and Node.js version 16 or setting NODE_OPTIONS="--openssl-legacy-provider" will resolve this issue.
If you're still experiencing problems, try restarting your PC or the Gradle daemon. This was causing issues because Gradle was always pointing to the initially configured Node.js version.