While building for android, sometimes there are errors. I've been working on react-native (these solutions will also work the same for flutter and other android dev languages) for quite sometime now and have faced errors throughtout but I have been documenting them locally so I'm creating this gist which I'll keep up-to-date as I face any new errors. Please note that all the solutions given here worked for me and they might not necessarily work for you so move with caution. Feel free to add your own solution that worked for you,
This occurs when your JDK version is not the same as mentioned in your project. To solve open the project in Android studio, make it finish its processing first then go to Preferences > Build, Execution, Deployment > Build Tools > Gradle and then Gradle Settings and select the correct JDK version i.e. it was JDK 8 in my case. Then try cleaning and building again.
Clean project, clean any build rel