Gradle: The filename, directory name, or volume label syntax is incorrect

Finally solved it. I looked at the error message again and tried to run the last command before failure, e.g.:

C:\Android\android-studio2\sdk\build-tools\android-4.4\dx.bat --dex --ou
tput E:\Backups\etc\TestProj2\Test2\build\libs\Test2-defaultFlavor-debug.dex E:\
Backups\etc\TestProj2\Test2\build\classes\defaultFlavor\debug E:\Backups\etc\Tes
tProj2\Test2\build\dependency-cache\defaultFlavor\debug

This gave the "The filename, directory name, or volume label syntax is incorrect." So then I went to the build-tools folder and ran dx.bat with no cmd-line args:

C:\Android\android-studio2\sdk\build-tools\android-4.4\dx.bat

And still got the same error. So I took a look inside the file and found these lines:

rem Check we have a valid Java.exe in the path.
set java_exe=
if exist    "%~dp0..\tools\lib\find_java.bat" call    "%~dp0..\tools\lib\find_java.bat"
if exist "%~dp0..\..\tools\lib\find_java.bat" call "%~dp0..\..\tools\lib\find_java.bat"
if not defined java_exe goto :EOF

So I tried running find_java.bat from the command line and it returned nothing.

So I replaced the above lines with the below one:

set java_exe=C:\Windows\System32\java.exe

And everything started working...