In many blogs, you will find that the solution is to downgrade to the API 30, however, if you want to experiment with the API 31, downgrading isn't a real solution. Fortunately, there's a way to make the build tools work for API 31. The cause of the corruption of the build tools for API 31 is 2 missing files:
These files exist however with another name, specifically:
So the workaround to prevent this exception from appearing is either to copy the mentioned files that exist with the new names or create a symbolic link (shortcut in Windows) and it will magically work. This will work in all the operative systems, however the way to do it will depend of every platform.
You can do this manually or with the command prompt, to create the symlink for
dx.bat (remember to replace the SDK directory with yours):
mklink C:\Users\username\AppData\Local\Android\Sdk\build-tools\31.0.0\dx.bat C:\Users\username\AppData\Local\Android\Sdk\build-tools\31.0.0\d8.bat
And the command to create the symlink for
mklink C:\Users\username\AppData\Local\Android\Sdk\build-tools\31.0.0\lib\dx.jar C:\Users\username\AppData\Local\Android\Sdk\build-tools\31.0.0\lib\d8.jar
After copying and renaming the mentioned files or running the previous commands to create the symbolic links (shortcuts), you will have new 2 files in the
MacOS and Linux
If you are using MacOS or Linux, running the following command in the terminal will do the trick (it switches to the build tools directory and then moves the d8 to dx and d8.jar to dx.jar):
cd ~/Library/Android/sdk/build-tools/31.0.0 && mv d8 dx && cd lib && mv d8.jar dx.jar
Try to build your project in Android Studio once again and the exception shouldn't appear anymore!
Happy coding ❤️!