Getting Build Failed "Installed Build Tools revision 31.0.0 is corrupted"

FAILURE: Build failed with an exception.
UATHelper: Packaging (Android (ETC2)): * What went wrong:
UATHelper: Packaging (Android (ETC2)): Could not determine the dependencies of task ‘:app:compileDebugJavaWithJavac’.
UATHelper: Packaging (Android (ETC2)): > Installed Build Tools revision 31.0.0 is corrupted. Remove and install again using the SDK Manager.
UATHelper: Packaging (Android (ETC2)): * Try:
UATHelper: Packaging (Android (ETC2)): Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
UATHelper: Packaging (Android (ETC2)): * Get more help at https://help.gradle.org
UATHelper: Packaging (Android (ETC2)): BUILD FAILED in 45s
UATHelper: Packaging (Android (ETC2)): ERROR: cmd.exe failed with args /c “D:\UnrealEngine\ExploreSScrol\Intermediate\Android\armv7\gradle\rungradle.bat” :app:assembleDebug
UATHelper: Packaging (Android (ETC2)): (see C:\Users\ASUS\AppData\Roaming\Unreal Engine\AutomationTool\Logs\D+Unreal+Engine+UE_4.27\Log.txt for full exception trace)
UATHelper: Packaging (Android (ETC2)): AutomationTool exiting with ExitCode=1 (Error_Unknown)
UATHelper: Packaging (Android (ETC2)): BUILD FAILED
PackagingResults: Error: Unknown Error

I have Installed To Two android studio on my machine

Mine was fixed after applying a change to the d8.bat file inside of the commandline tools you are working with (C:\Users\[USERNAME]\AppData\Local\Android\Sdk\build-tools\31.0.0 for example).

Navigate to this folder, and make sure that you have dx.bat listed there. If not, rename d8.bat to dx.bat and then try again. Should be good to go after that!

I don’t know why this works, tutorials for building android on UE4 show this as a step. From downloading older command-line tools versions, it seems that d8.bat and dx.bat used to be packaged into command-line tools. So I imagine this is an old compatibility things where they stopped shipping dx.bat, but Unreal still looks for it as it was built for an older version. This is just my hypothesis, not exactly sure why this works the way it does.