Could not find method android() for arguments

In android studio, when trying to “run” a project created by “ionic cap build android”, I received the following error: failed.
Could not find method android() for arguments [capacitor_build_5inrsddqrcw9wy0$_run_closure1@66f27ce8] on root project ‘app’ of type org.gradle.api.Project.

When I tried to “debug” the project, I got some message like:
The target JRE version 11.0.10 doesn’t match the project jdk…

When I ran “ionic capacitor run android”, I received error message:
[capacitor] ✔ update android in 408.55ms
[capacitor] 2021-10-02T10:34:44.415Z capacitor:android:run Invoking ./gradlew with args: [ ‘assembleDebug’ ]
[capacitor] ✖ Running Gradle build - failed!
[ERROR] An error occurred while running subprocess capacitor.

capacitor run android --target xxxxxx exited with exit code 1.

Re-running this command with the --verbose flag may provide more
ionic:utils-process onBeforeExit handler: ‘process.exit’ received +0ms
ionic:utils-process onBeforeExit handler: running 2 functions +0ms
ionic:utils-process processExit: exiting (exit code: 1) +54ms
My ionic info output:

Ionic CLI : 6.17.1 (/usr/local/lib/node_modules/@ionic/cli)
Ionic Framework : @ionic/angular 5.8.1
@angular-devkit/build-angular : 12.1.4
@angular-devkit/schematics : 12.1.4
@angular/cli : 12.1.4
@ionic/angular-toolkit : 4.0.0


Capacitor CLI : 3.2.4
@capacitor/android : 3.2.4
@capacitor/core : 3.2.4
@capacitor/ios : 3.2.4


cordova-res : 0.15.3
native-run : 1.4.1


NodeJS : v14.18.0 (/usr/local/bin/node)
npm : 6.14.15
OS : macOS Big Sur

Any help would be much appreciated.

Can anyone share what version of gradle, JDK, android sdk work well with capacitor 3.2.4? thx.
android build is a real pain every time.

Finally Figured it out:

Android gradle plugin version 7.0.2
Android gradle version 7.0.2
Java SE development Kit 16 for Mac OS(big sur)

I hope it’ll help those who’s struggling as well. Make sure, you go to Android Studio, File, Project Structure, Project and both version are 7.0.2, if not, upgrade. As for Java SDK, it works for me version 16, not sure about 17. I actually downgraded from 17 to 16. Maybe 17 also works. I’m not sure.

ionic used to be quite straightforward, switching to capacitor brings the good stuff but also pain. And I couldn’t find any useful document which works for deploying to Android.