I'd like to make some additions to mxkmn's answer above https://stackoverflow.com/a/77736735/12131861
If you are using MacOS or by some reason something goes wrong, AS might throw an error like this
Cannot run program "/Users/.../gradlew.bat" (in directory "/Users/..."): error=13, Permission denied
So you should replace the "Program field" in external tools dialog to $ProjectFileDir$/gradlew
instead of $ProjectFileDir$/gradlew.bat
If you keep getting the same error, try to execute the command below in terminal to make the gradlew file executable
chmod +x gradlew