To fix this error in Android Studio, follow any of the below approaches.
1. Creating a symlink
You can use command line and create symlink references from dx.bat to d8.bat, dx.jar to d8.jar
Windows:
mklink %SDK_PATH%\build-tools\31.0.0\dx.bat %SDK_PATH%\build-tools\31.0.0\d8.bat && mklink %SDK_PATH%\build-tools\31.0.0\lib\dx.jar %SDK_PATH%\build-tools\31.0.0\lib\d8.jar
Mac:
cd /sdk/build-tools/31.0.0
ln -s dx.bat d8.bat
ln -s lib/dx.jar lib/d8.jar
2. Renaming the dx.bat and dx.jar files in build tool 31.0.0
Step 1: Open the SDK folder in file explorer and navigate to build-tools\31.0.0. If you are not sure of the SDK path, then go to the Android SDK manager and check the SDK location.

Step 2: Rename the d8.bat file as dx.bat in sdk\build-tools\31.0.0\dx.bat
Step 3: Navigate to sdk\build-tools\31.0.0\lib folder and rename d8.jar as dx.jar
You can use below command line to move files
cd ~/sdk/build-tools/31.0.0 && mv d8 dx && cd lib && mv d8.jar dx.jar